Jump to content

  • Log In with Google      Sign In   
  • Create Account


Cannot allocate a decent amount of memory glBufferData()


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 thecheeselover   Members   -  Reputation: 308

Like
0Likes
Like

Posted 03 February 2014 - 03:36 PM

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


Hide yo cheese! Hide yo wife!

Sponsor:

#2 richardurich   Members   -  Reputation: 1187

Like
4Likes
Like

Posted 03 February 2014 - 06:05 PM

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.



#3 thecheeselover   Members   -  Reputation: 308

Like
0Likes
Like

Posted 05 February 2014 - 10:23 AM

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.

Hide yo cheese! Hide yo wife!

#4 JTippetts   Moderators   -  Reputation: 8159

Like
2Likes
Like

Posted 05 February 2014 - 10:44 AM

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



#5 thecheeselover   Members   -  Reputation: 308

Like
0Likes
Like

Posted 06 February 2014 - 06:33 PM

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);
}


Hide yo cheese! Hide yo wife!

#6 richardurich   Members   -  Reputation: 1187

Like
1Likes
Like

Posted 06 February 2014 - 08:53 PM

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.



#7 Hodgman   Moderators   -  Reputation: 27786

Like
4Likes
Like

Posted 06 February 2014 - 11:21 PM

What is vertexData?

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



#8 thecheeselover   Members   -  Reputation: 308

Like
0Likes
Like

Posted 07 February 2014 - 07:21 AM

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};

Hide yo cheese! Hide yo wife!

#9 thecheeselover   Members   -  Reputation: 308

Like
0Likes
Like

Posted 07 February 2014 - 07:23 AM

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?


Hide yo cheese! Hide yo wife!

#10 Juliean   GDNet+   -  Reputation: 2243

Like
2Likes
Like

Posted 07 February 2014 - 09:36 AM

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, 07 February 2014 - 09:39 AM.


#11 richardurich   Members   -  Reputation: 1187

Like
1Likes
Like

Posted 07 February 2014 - 11:13 AM

 

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)



#12 thecheeselover   Members   -  Reputation: 308

Like
0Likes
Like

Posted 07 February 2014 - 12:53 PM

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);

Hide yo cheese! Hide yo wife!

#13 richardurich   Members   -  Reputation: 1187

Like
1Likes
Like

Posted 07 February 2014 - 02:12 PM

 

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS