I seem to be having trouble rendering my static with optimal performance. I am creating my static buffers with write only flags and locking them only once to store my geometry passed in during program initialization. Here is the code that I use to transfer geometry to a static buffer.
HRESULT CFNGED3D::CreateBuffer(DWORD dwFormat, int nStride, int nVertices, void* pVertices, int nIndices, void* pIndices, int nMaterialID, int* pBufferID)
{
// First check to see if the passed in material id is valid.
if (nMaterialID < 0 || nMaterialID > m_materials.size())
{
return E_INVALIDARG;
}
FNGE_D3D_BUFFER tempBuffer;
tempBuffer.m_dwFormat = dwFormat;
tempBuffer.m_nStride = nStride;
tempBuffer.m_nVertices = nVertices;
tempBuffer.m_nIndices = nIndices;
tempBuffer.m_nMaterialID = nMaterialID;
void* pData;
// Now create the vertex buffer based on the stride and number of vertices specified.
if (FAILED(m_pD3DDevice->CreateVertexBuffer(tempBuffer.m_nVertices * tempBuffer.m_nStride,
D3DUSAGE_WRITEONLY,
tempBuffer.m_dwFormat,
D3DPOOL_DEFAULT,
&tempBuffer.m_pVB,
NULL)))
{
}
if (FAILED(tempBuffer.m_pVB->Lock(0, 0, (void**)&pData, 0)))
{
}
memcpy(pData, pVertices, tempBuffer.m_nVertices * tempBuffer.m_nStride);
tempBuffer.m_pVB->Unlock();
if (tempBuffer.m_nIndices > 0)
{
if (FAILED(m_pD3DDevice->CreateIndexBuffer(tempBuffer.m_nIndices * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_DEFAULT,
&tempBuffer.m_pIB,
NULL)))
{
}
if (FAILED(tempBuffer.m_pIB->Lock(0, 0, (void**)&pData, 0)))
{
}
memcpy(pData, pIndices, tempBuffer.m_nIndices * sizeof(WORD));
tempBuffer.m_pIB->Unlock();
}
(*pBufferID) = m_staticBuffers.size();
m_staticBuffers.push_back(tempBuffer);
m_Log << "A new static buffer has been created and added to the engine.";
return S_OK;
}
Here is the code I use to render them
void CFNGED3D::Render(int nBufferID)
{
if (nBufferID < 0 || nBufferID > m_staticBuffers.size())
return;
// Determine whether or not this computer supports vertex shaders.
if (m_deviceCaps.m_isShaderSupport)
{
m_pD3DDevice->SetFVF(NULL);
}
else
{
m_pD3DDevice->SetFVF(m_staticBuffers[nBufferID].m_dwFormat);
}
//D3DMATERIAL9 mat;
//mat.Diffuse.a = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Diffuse.A;
//mat.Diffuse.r = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Diffuse.R;
//mat.Diffuse.g = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Diffuse.G;
//mat.Diffuse.b = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Diffuse.B;
//mat.Ambient.a = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Ambient.A;
//mat.Ambient.r = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Ambient.R;
//mat.Ambient.g = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Ambient.G;
//mat.Ambient.b = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Ambient.B;
//mat.Specular.a = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Specular.A;
//mat.Specular.r = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Specular.R;
//mat.Specular.g = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Specular.G;
//mat.Specular.b = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Specular.B;
//mat.Emissive.a = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Emissive.A;
//mat.Emissive.r = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Emissive.R;
//mat.Emissive.g = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Emissive.G;
//mat.Emissive.b = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Emissive.B;
//mat.Power = m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_Power;
//m_pD3DDevice->SetMaterial(&mat);
//
//// Assign each texture assigned to this material to each respective stage.
//for (int i = 0; i < 5; i++)
//{
// if (m_materials[nBufferID].m_textureID > -1)
// {
// LPDIRECT3DTEXTURE9 pTexture = (LPDIRECT3DTEXTURE9)m_textures[m_materials[m_staticBuffers[nBufferID].m_nMaterialID].m_textureID].m_pData;
// m_pD3DDevice->SetTexture(i, pTexture);
// }
//}
m_pD3DDevice->SetStreamSource(0, m_staticBuffers[nBufferID].m_pVB, 0, m_staticBuffers[nBufferID].m_nStride);
if (m_staticBuffers[nBufferID].m_nIndices > 0)
{
m_pD3DDevice->SetIndices(m_staticBuffers[nBufferID].m_pIB);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, m_staticBuffers[nBufferID].m_nVertices, 0, m_staticBuffers[nBufferID].m_nVertices / 3);
}
else
{
m_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, m_staticBuffers[nBufferID].m_nVertices / 3);
}
}
With the code I am using right now for managing static buffers I can only rendering 12k vertices with a framerate of 20fps. I know that frames per second is a bad scale for judging program performance because it scales non-linearly. However I have seen many programs which can render 100x the amount of vertices I am rendering and still get a better frame rate. Does anybody know why I might be seeing this massive performance drop.