user3228136
user3228136

Reputation: 167

Can OpenGL VBOs leak to CPU RAM?

I'm using glGenBuffers to create a large amount of STATIC_DRAW objects (a terrain, towns and trees) on the GPU. I'm uploading them at the start of the program, to be deleted at the end. As I allocate the buffers on the GPU, my CPU RAM usage grows continuosly. From around 700 MB it reaches 2 GB. It seems that as I create the buffers, they are stored both in the GPU and the CPU. I am not using dynamically allocated memory. Here is some code:

for(auto& S : mW.Set) { //Iterates through every SETtlement in the mainWorld
    float F[24] = {-0.5, 0.5, 0.5,     -0.5, -0.5, 0.5,     0.5, -0.5, 0.5,     0.5, 0.5, 0.5,
                -0.5, 0.5, -0.5,    -0.5, -0.5, 0.5,     0.5, -0.5, 0.5,     0.5, 0.5, 0.5}; //8 vertices - defining a cube
    unsigned short I[30] =  {0, 1, 2, 0, 2, 3,
                            0, 1, 4, 0, 4, 5,
                            0, 3, 7, 0, 4, 7,
                            2, 3, 6, 3, 6, 7,
                            1, 2, 5, 2, 5, 6}; //30 indices - bottom face deliberately missing
    for(int i=0; i<24; ++i) { //Manual scaling and translation of the object
        if(((i + 1) % 3) == 1) {
            F[i] *= S->MapSize;
            F[i] += S->X;
        }
        else if(((i + 1) % 3) == 2) {
            F[i] *= S->MapSize;
            F[i] += S->Y;
        }
        else if(((i + 1) % 3) == 0) {
            F[i] *= .1;
            F[i] += (mW.GTile[S->X][S->Y].Point[0]->Position.Z + mW.GTile[S->X][S->Y].Point[1]->Position.Z + mW.GTile[S->X][S->Y].Point[2]->Position.Z + mW.GTile[S->X][S->Y].Point[3]->Position.Z) / 4;
            F[i] += 0.5;
        }
    }
    glGenBuffers(1, &S->buffObj.Array); //Each Settlement (S) object has a buffObj struct which contains a GPU pointer to the vertices and indexes
    glBindBuffer(GL_ARRAY_BUFFER, S->buffObj.Array);
    glBufferData(GL_ARRAY_BUFFER, sizeof(F), F, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glGenBuffers(1, &S->buffObj.Index);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, S->buffObj.Index);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(I), I, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} //Every for loop iteration it leaks heavily
//The whole for loops takes 1-2 seconds of execution, and adds around 300 MB of CPU RAM usage, which is very hard to explain, since there is no permanent allocation of memory in the RAM, just in the VRAM. 

I am using C++, VS2012 and OpenGL 3.3.

Upvotes: 0

Views: 766

Answers (1)

ratchet freak
ratchet freak

Reputation: 48216

The driver is free to decide where the VBO is stored and free to move it around as it sees fit.

This means that it's possible that each call to glBufferData will malloc a buffer and the data gets memcopied there until it's needed for a draw call when it is copied to the GPU RAM (after which it can be purged from RAM/the buffer can be reused). The hints tell the driver that you don't plan to read the data out again with glGetBufferSubData or change it with glBufferSubData, but it should still be ready if you do do either one.

Upvotes: 1

Related Questions