• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
thecheeselover

Cannot allocate a decent amount of memory glBufferData()

12 posts in this topic

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

0

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.
0

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

0

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.

1

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

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?

0

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
2

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)

1

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

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.

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0