I've been using glBufferData
, and it makes sense to me that you'd have to specify usage hints (e.g. GL_DYNAMIC_DRAW).
However, it was recently suggested to me on Stack Overflow that I use glMapBuffer
or glMapBufferRange
to modify non-contiguous blocks of vertex data.
When using glMapBuffer
, there does not seem to be any point at which you specify a usage hint. So, my questions are as follows:
glMapBuffer
on a given VBO if you've never called glBufferData
on that VBO?glMapBuffer
vs glBufferData
? (I know they don't do exactly the same thing. But it seems that by getting a pointer with glMapBuffer
and then writing to that address, you can do the same thing glBufferData
does.)glDeleteBuffers deletes n buffer objects named by the elements of the array buffers . After a buffer object is deleted, it has no contents, and its name is free for reuse (for example by glGenBuffers). If a buffer object that is currently bound is deleted, the binding reverts to 0 (the absence of any buffer object).
To create a buffer object, you call glGenBuffers. Deleting them uses glDeleteBuffers. These use the standard Gen/Delete paradigm as most OpenGL objects. The target defines how you intend to use this binding of the buffer object.
Description. glBufferData creates a new data store for the buffer object currently bound to target . Any pre-existing data store is deleted. The new data store is created with the specified size in bytes and usage . If data is not NULL , the data store is initialized with data from this pointer.
- Is it valid to use glMapBuffer on a given VBO if you've never called glBufferData on that VBO?
No, because to map some memory, it must be allocated first.
- If so, how does OpenGL guess the usage, since it hasn't been given a hint?
It doesn't. You must call glBufferData at least once to initialize the buffer object. If you don't want to actually upload data (because you're going to use glMapBuffer), just pass a null pointer for the data pointer. This works just like with glTexImage, where a buffer/texture object is created, to be filled with either glBufferSubData/glTexSubImage, or in the case of a buffer object as well as through a memory mapping.
- What are the advantages/disadvantages of glMapBuffer vs glBufferData? (I know they don't do exactly the same thing. But it seems that by getting a pointer with glMapBuffer and then writing to that address, you can do the same thing glBufferData does.)
glMapBuffer
allows you to write to the buffer asynchronously from another thread. And for some implementations it may be possible, that the OpenGL driver gives your process direct access to DMA memory of the GPU or even better to the memory of the GPU itself. For example on SoC architectures with integrated graphics.
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