VOID VertexData::AppendVertexDecl( LPDIRECT3DVERTEXDECLARATION9 pVertexDecl, DWORD dwStreamIndex ) { if( m_pVertexDecl == NULL ) { SetVertexDecl( pVertexDecl ); return; } DWORD dwExistingCount = MAXD3DDECLLENGTH + 1; D3DVERTEXELEMENT9 ExistingElements[MAXD3DDECLLENGTH + 1]; m_pVertexDecl->GetDeclaration( ExistingElements, (UINT*)&dwExistingCount ); DWORD dwNewCount = MAXD3DDECLLENGTH + 1; D3DVERTEXELEMENT9 NewElements[MAXD3DDECLLENGTH + 1]; pVertexDecl->GetDeclaration( NewElements, (UINT*)&dwNewCount ); AppendVertexElements( ExistingElements, dwStreamIndex, NewElements, 0, 0 ); D3DVertexDeclaration* pNewDecl = D3DDevice_CreateVertexDeclaration( ExistingElements ); SetVertexDecl( pNewDecl ); }
//-------------------------------------------------------------------------------------- // Name: CreatePooledVertexDeclaration() // Desc: Function to coalesce vertex declarations into a shared pool of vertex declarations //-------------------------------------------------------------------------------------- HRESULT WINAPI CreatePooledVertexDeclaration( const D3DVERTEXELEMENT9* pVertexElements, D3DVertexDeclaration** ppVertexDeclaration ) { static std::vector <VTX_DECL> m_VertexDecls; // Count the number of vertex elements DWORD dwNumElements = 0; while( pVertexElements[dwNumElements].Stream < 16 ) dwNumElements++; assert( dwNumElements <= MAXD3DDECLLENGTH ); // Check for a previously-created vertex decl for( unsigned int i = 0; i < m_VertexDecls.size(); i++ ) { if( ( m_VertexDecls[i].dwNumElements == dwNumElements ) && ( !memcmp( m_VertexDecls[i].pElements, pVertexElements, sizeof( D3DVERTEXELEMENT9 ) * dwNumElements ) ) ) { // If found, return it ( *ppVertexDeclaration ) = m_VertexDecls[i].pDeclaration; return S_OK; } } // No previously-created vertex decl was found, so create one ( *ppVertexDeclaration ) = D3DDevice_CreateVertexDeclaration( pVertexElements ); // And save a record of it VTX_DECL d; XMemCpy( d.pElements, pVertexElements, sizeof( D3DVERTEXELEMENT9 ) * dwNumElements ); d.dwNumElements = dwNumElements; d.pDeclaration = ( *ppVertexDeclaration ); d.pDeclaration->AddRef(); m_VertexDecls.push_back( d ); return S_OK; }