I'm studying OpenGL API and I would like to ask you if I can delete a float array of vertices after passing it to OpenGL.
Example code:
GLuint VBO;
float *vertices = new float[2];
vertices[0] = 0.0f;
vertices[1] = 1.0f;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
delete[] vertices;
Could you tell me consequences about doing it?
Yes, absolutely. After the glBufferData()
call returns, you can do anything you want with the data. Overwrite it, delete it, etc.
The consequence is that the OpenGL implementation either has to update the buffer immediately during the call (which is somewhat against the asynchronous way that OpenGL likes to operate in), or create a temporary copy of the data (which is undesirable for performance reasons).
This is a main reason why calls like glMapBufferRange()
were introduced. They avoid the extra data copy that often happens under the hood when glBufferData()
and glBufferSubData()
are used. They have their own synchronization caveats if not used carefully.
There is a very similar case for texture data, where calls like glTexImage2D()
often cause an extra copy of the data in the OpenGL implementation. The performance implications can be worse in that case because texture data is typically much bigger. Apple has an extension for this purpose (APPLE_client_storage), where you can avoid the extra copy by promising to keep the data untouched until it has been consumed by OpenGL.
Almost all OpenGL calls with data pointers as arguments consume the data as part of the call. The only notable exception I can think of is glVertexAttribPointer()
when used without VBOs. This usage is often called "client side vertex arrays", and the data is consumed during the draw call, which can be long after the glVertexAttribPointer()
call. This usage is deprecated, and not available in the OpenGL Core Profile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With