3
\$\begingroup\$

I have a vertex struct that has 5 glm::vec3 but some of my objects only use 2 or 3 of those members. So I have two questions :

1.) What happens to the large buffer I create, even though I don't use all the data.

Struct Vertex {
          glm::vec3 pos, color, UV, normal, tangent
              };
std::vector<Vertex> vertices; // will store the vertex data
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), vertices.data(), GL_STATIC_DRAW);

That creates a buffer that is larger than needs to be. Is the additional buffer data not send?

2.) If I send empty data to the buffers, is it causing any performance issues?

glVertexPointer(2, 3 , GL_FLOAT, GL_FALSE, size of(Vertex), reinterpret_cast<GLvoid*>(Offsetof(Vertex, unusedData)); 

Thanks for the information!

\$\endgroup\$
1
\$\begingroup\$

That creates a buffer that is larger than needs to be. Is the additional buffer data not send?

glBufferData creates a buffer of the specified size, no matter what. If you want a smaller buffer, specify a smaller size.

It doesn't copy any data unless you pass a non-null value to the data parameter, in which case it will copy all of it (where "all" is "the size of the buffer") internally to use for initialization.

It is too early at this point for OpenGL to perform the kind of "optimization" you are wondering about, even if it were possible (in your case it would be impractical since you have intermixed your "used" and "unused" vertex fields into the same buffer anyway). And as a sort of general rule, that kind of higher-level management decision is something OpenGL leaves to you.

If I send empty data to the buffers, is it causing any performance issues?

If you routinely find yourself with a buffer where you are only using a subset of the vertex data, it's probably worth investigating whether or you not you can

  • create a new buffer with a new vertex format containing only the data you need or
  • spread the vertex data across multiple buffers instead of interleaving it in one

There is likely, technically, to be a small performance hit in terms of how efficiently the GPU can cache vertex data, as you'd be wasting a lot of its cache on data not actually used by the shaders. The bigger issue is simply the fact that you're routinely ignoring the data you've put into the GPU's memory, so you're just kind of wasting the memory.

If you can compact your vertex format or otherwise spread it across multiple buffers you don't always need mapped, that may be a net win. But it's difficult to say for sure, as those changes have their own performance wins and loses as well, depending on the context of your use of the features and the way your application processes data, et cetera.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.