• Advertisement
Sign in to follow this  

Cannot allocate a decent amount of memory glBufferData()

This topic is 1446 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I have an issue with glBufferData() and memory. I was testing how much memory I could allocate to a single vbo. I noticed it was smaller than 10 KB (10 000 bytes) and bigger than 1 KB (1000 bytes). The problem is that I have approximately 1700 MB of memory on my GPU and 8 GB of RAM on my computer which is way more than 10 KB...

 

Thank you,

Thecheeselover

Share this post


Link to post
Share on other sites
Advertisement

Is it returning GL_OUT_OF_MEMORY or GL_INVALID_OPERATION or what? Considering you want to aim for buffers over a megabyte in size, it's definitely odd to hear you can't allocate 10k. Without knowing anything about your code and such, I have no clue what to tell you though.

Share this post


Link to post
Share on other sites

Is it returning GL_OUT_OF_MEMORY or GL_INVALID_OPERATION or what? Considering you want to aim for buffers over a megabyte in size, it's definitely odd to hear you can't allocate 10k. Without knowing anything about your code and such, I have no clue what to tell you though.

gDEBugger shows me this:

 

Exception
Event Properties
Reason: Access violation
Address: 0x52f19f89
Details: The thread tried to read from or write to a virtual address to which it does not have access.

Share this post


Link to post
Share on other sites

Post your code. That's a segfault, not an out-of-memory exception.

void MinecraftTest1::InitBuffers()
{
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
    // glBufferData(GL_ARRAY_BUFFER, 10000, vertexData, GL_STATIC_DRAW);

    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertexIndices), vertexIndices, GL_STATIC_DRAW);
}

Share this post


Link to post
Share on other sites

If the commented out line is the one that crashes, that's because you're copying past the end of vertexData. glBufferData (using text replacement with your code) is basically:

boundBuffer[GL_ARRAY_BUFFER] = malloc(10000);

memcpy(boudBuffer[GL_ARRAY_BUFFER], vertexData, 10000);

 

And assuming vertexData is less than 10000 bytes long, the memcpy part of that crashes after trying to copy past the end of vertexData.

Share this post


Link to post
Share on other sites

What is vertexData?

sizeof(vertexData) is probably not what you think it is; you're probably allocating a 4-byte VBO.

Here it is:
 

GLfloat vertexData[NUM_VERTS * ELEM_PER_POS + NUM_VERTS * ELEM_PER_COLOR + NUM_VERTS * ELEM_PER_NORM] = {
    -1.0f, -1.0f, -1.0f, // Positions
    -1.0f, 1.0f, -1.0f,  // Back
    1.0f, 1.0f, -1.0f,
    1.0f, -1.0f, -1.0f,

    -1.0f, -1.0f, 1.0f,  // Front
    -1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f,
    1.0f, -1.0f, 1.0f,

    1.0f, 1.0f, 1.0f, 1.0f, // Colors
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,

    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 1.0f,

    -1.0f, -1.0f, -1.0f, // Normals
    -1.0f, 1.0f, -1.0f,  // Back
    1.0f, 1.0f, -1.0f,
    1.0f, -1.0f, -1.0f,

    -1.0f, -1.0f, 1.0f,  // Front
    -1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f,
    1.0f, -1.0f, 1.0f};

Share this post


Link to post
Share on other sites

If the commented out line is the one that crashes, that's because you're copying past the end of vertexData. glBufferData (using text replacement with your code) is basically:

boundBuffer[GL_ARRAY_BUFFER] = malloc(10000);

memcpy(boudBuffer[GL_ARRAY_BUFFER], vertexData, 10000);

 

And assuming vertexData is less than 10000 bytes long, the memcpy part of that crashes after trying to copy past the end of vertexData.

Possible, but why allocating 1000 bytes doesn't crash?

Share this post


Link to post
Share on other sites

As Hodgeman has already pointed out and as your code clearly shows, you are allocating way to less memory. sizeof(vertexData) returns the size of the datatype of vertexData, which is GLfloat, which is 4 byte. You need sizeof(float)*numVertices*numElementsPerVertex in order allocate a correctly sized buffer.

 

Though I'm no expert on the details, the reason why having less bytes not crash is that behaviour is somewhat undefined if violating memory boundaries. For example, a fellow colleque of mine had a fixed-size texture cache with an array of size 23, but she did in fact store 25 textures without any problem whatsoever. I at some point deleted a dynamic array without [] and didn't get a crash until like a week later in some unrelated code piece. I also once deleted memory twice and got some strange DirectX11-related error from the GPU that didn't have anything to do with the issue itself. The thing is, when you are writing over the boundaries of like an array, you are writing to some random region of memory, where eigther nothing can be allocated yet, or you could overwrite something from another array, etc... if you are lucky, the debugger catches on to it, if not, such random things as in your case happen. Bottom line: Memory is stupid, always triple-check when dealing with raw memory management.

Edited by Juliean

Share this post


Link to post
Share on other sites

 

If the commented out line is the one that crashes, that's because you're copying past the end of vertexData. glBufferData (using text replacement with your code) is basically:

boundBuffer[GL_ARRAY_BUFFER] = malloc(10000);

memcpy(boudBuffer[GL_ARRAY_BUFFER], vertexData, 10000);

 

And assuming vertexData is less than 10000 bytes long, the memcpy part of that crashes after trying to copy past the end of vertexData.

Possible, but why allocating 1000 bytes doesn't crash?

 

The same reason memcpy doesn't always crash when you copy past your allocated area.

 

 

As Hodgeman has already pointed out and as your code clearly shows, you are allocating way to less memory. sizeof(vertexData) returns the size of the datatype of vertexData, which is GLfloat, which is 4 byte. You need sizeof(float)*numVertices*numElementsPerVertex in order allocate a correctly sized buffer.

This would be correct if he dynamically allocated vertexData, but vertexData is a fixed size array. sizeof(float[200]) is 800, not 4. (assuming 4-byte floats and 4-byte pointers)

Share this post


Link to post
Share on other sites

 

To summarize, this is how my code should look like?
 

glBufferData(GL_ARRAY_BUFFER, NUM_VERTS * sizeof(GLfloat) * (ELEM_PER_POS + ELEM_PER_COLOR + ELEM_PER_NORM),
                  vertexData, GL_STATIC_DRAW);

It's fine to use sizeof(vertexData) as long as you're not dynamically allocating it (new, malloc, etc.). You will eventually outgrow that when you dynamically load vertex data from files. When you do, you'll basically use sizeof(single element) * number of elements, so it might be "sizeof(vertexData[0]) * vertexData.size()" if you're using stl::vector to store your vertices.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement