Original Post
Hi there, I am writing a 2d Engine in dx9 and I am facing a, at least, funny side effects at the sprite blitting routines. As you can read everywhere in the net, everybody recommends you set the texture, batch the vertices and than call the DrawPrimitive() to finally send your tris to the GPU. Well I do that and realized that this way is slower, I’ll write again, slower than the old tests I did, where each sprite seted its texture and called DrawPrimitive(). I am sure something is wrong in my code, but I cannot find where. Can someone please help me? Tanx. Here is the code:
//------------------------------------------------------------------------------
inline void cSpriteGroup::BeginRender( LPDIRECT3DDEVICE9 pDevice )
{
pDevice->SetTexture( 0, m_pTexture );
pDevice->SetStreamSource( 0, m_pVertexBuffer, 0, sizeof( CUSTOMVERTEX ) );
}
//------------------------------------------------------------------------------
inline void cSpriteGroup::EndRender( LPDIRECT3DDEVICE9 pDevice )
{
if( m_bBufferLocked ) FlushVertexBuffer( pDevice );
}
//------------------------------------------------------------------------------
void cSpriteGroup::BlitSprite( cSprite* sprite, LPDIRECT3DDEVICE9 pDevice )
{
if( !m_bBufferLocked )
{
m_pVertexBuffer->Lock( 0, 0, ( void** )&m_pVertex, D3DLOCK_DISCARD );
m_bBufferLocked = true;
}
m_pVertex->m_vPos.x = sprite->m_fPosX;
m_pVertex->m_vPos.y = sprite->m_fPosY;
m_pVertex->m_vPos.z = 0;
m_pVertex->m_vPos.w = 1.0f;
m_pVertex->m_vTex.x = sprite->m_xUVs[sprite->m_iCurFrame].l;
m_pVertex->m_vTex.y = sprite->m_xUVs[sprite->m_iCurFrame].t;
m_pVertex->diffuse = sprite->m_dwColor;
m_pVertex++;
//... all the other 5 vertices are also filled here = 6 vertices per tile
m_dwVerticesCount += 6;
if( m_dwVerticesCount == m_dwMaxVertices ) FlushVertexBuffer( pDevice );
}
//------------------------------------------------------------------------------
void cSpriteGroup::FlushVertexBuffer( LPDIRECT3DDEVICE9 pDevice )
{
m_pVertexBuffer->Unlock();
pDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, m_dwVerticesCount / 3 );
m_dwVerticesCount = 0;
m_pVertex = 0;
m_bBufferLocked = false;
}
//------------------------------------------------------------------------------