我使用glGenBuffers在GPU上创建了大量的STATIC_DRAW对象(地形,城镇和树木)。我在程序开始时上传它们,最后要删除它们。当我在GPU上分配缓冲区时,我的CPU RAM使用量持续增长。从大约700 MB到2 GB。似乎在创建缓冲区时,它们都存储在GPU和CPU中。我没有使用动态分配的内存。这是一些代码:
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.
我正在使用C ++,VS2012和OpenGL 3.3。
答案 0 :(得分:1)
驱动程序可以自由决定VBO的存储位置,并在其认为合适的情况下随意移动它。
这意味着每次调用glBufferData都可能会对缓冲区进行malloc,并且数据会在那里进行memcopied,直到它被复制到GPU RAM时需要进行绘制调用(之后它可以从RAM中清除/缓冲区可以重复使用)。提示告诉司机您不打算使用glGetBufferSubData
再次读取数据或使用glBufferSubData
更改数据,但如果您执行任何一项操作,它仍应准备好。