سؤال

Over the past few days I made my first "engine" thingy. A central object with a window object, graphics object, and an input object - all nice and encapsulated and happy. In this setup I also included some objects in the graphics object that handle some 'utility' functions, like a camera and a 'vindex' manager.

The Vertex/Index Manager stores all vertices and indices in std::vectors, that are called upon and sent to graphics when it's time to create the buffers.

The only problem is that I get ~8 frames a second with only 8-10 rectangles. I think the problem is in the 'Vindex' object (my shader is nothing spectacular, and the pipeline is pretty vanilla).

Is storing Vertices in this way a plum bad idea, or is there just some painfully obvious thing I'm missing?

I did a little evolution sim project a few years ago that was pretty messy code-wise, but it rendered 20,000 vertices at 100s of frames a second on this machine, so it's not my machine that's slow.

I've been kind of staring at this for several hours, any and all input is VERY much appreciated :)

Example from my object that stores my vertices:

for (int i = 0; i < 24; ++i)
{
    mVertList.push_back(Vertex(v[i], n[i], col));
}

For Clarity's sake

std::vector<Vertex> mVertList;
std::vector<int> mIndList;

and

std::vector<Vertex> VindexPile::getVerts()
{
    return mVertList;
}
std::vector<int> VindexPile::getInds()
{
    return mIndList;
}

In my graphics.cpp file:

md3dDevice->CreateVertexBuffer(mVinds.getVerts().size() * sizeof(Vertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mVB, 0);
Vertex * v = 0;
mVB->Lock(0, 0, (void**)&v, 0);

std::vector<Vertex> vList = mVinds.getVerts();

for (int i = 0; i < mVinds.getVerts().size(); ++i)
{
    v[i] = vList[i];
}

mVB->Unlock();


md3dDevice->CreateIndexBuffer(mVinds.getInds().size() * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &mIB, 0);

WORD* ind = 0;
mIB->Lock(0, 0, (void**)&ind, 0);

std::vector<int> iList = mVinds.getInds();

for (int i = 0; i<mVinds.getInds().size(); ++i)
{
    ind[i] = iList[i];
}

mIB->Unlock();
هل كانت مفيدة؟

المحلول

There is quite a bit of copying going on in here: I can not tell without running a profiler and some more code, but that seems like the first culprit:

std::vector<Vertex> vList = mVinds.getVerts();
std::vector<int> iList = mVinds.getInds();

Those two calls create copies of your vertex/index buffers, which is most probably not what you want - you most probably want to declare those as const references. You are also ruining cache coherency by doing those copies, which slows down your program more.

mVertList.push_back(Vertex(v[i], n[i], col));

This is moving and resizing the vectors quite a lot as well - you should most probably use reserve or resize before putting stuff in your vectors, to avoid reallocation and moving throughout memory of your data.

If I have to give you one big advice however, that would be: Profile. I don't know what tools you have access to, however there are plenty of profilers available, pick one and learn it, and it will provide much more valuable insight into why your program is slow.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top