PDA

View Full Version : binding the same buffer object to a different target



driver
06-17-2014, 10:30 AM
Hi

Can one bind the same buffer object to different targets at differnt times?


So for example, let's say I create a buffer
glGenBuffers(1, &id);
glBindBuffer(GL_ARRAY_BUFFER,id);
glBufferData(GL_ARRAY_BUFFER, 2048, data, GL_DYNAMIC_COPY); //data is my data array


Let's say I use this to do certain things.

Now I want to use the same buffer as a GL_SHADER_STORAGE_BUFFER, can I simply go ahead and use the sae buffer without having to recreate another buffer?
glBindBuffer(GL_SHADER_STORAGE_BUFFER,id);



For some reason, I had stumbled on this question earlier, but I cannot remember what I had learned from it.


thanks

mhagain
06-17-2014, 11:57 AM
Yes, you can do this.

However, buffer objects have their origins in the GL_ARB_vertex_buffer_object (http://www.opengl.org/registry/specs/ARB/vertex_buffer_object.txt) extension, and that notes:


Note that it is expected that implementations may have different memory type requirements for efficient storage of indices and vertices. For example, some systems may prefer indices in AGP memory and vertices in video memory, or vice versa; or, on systems where DMA of index data is not supported, index data must be stored in (cacheable) system memory for acceptable performance. As a result, applications are strongly urged to put their models' vertex and index data in separate buffers, to assist drivers in choosing the most efficient locations.

I'm not sure how much of this is relevant to more modern hardware, but if so, it can be expected to be generalized to any buffer object type. So the standard advice is that GL may make assumptions about how (and where) best to store the buffer based on the binding-point used when it's first bound, but that these assumptions may not yield the best performance for other binding-points. On the other hand if it saves you a memory copy then that may not be such a big deal, so I'd encourage you to experiment with both approaches (never forgetting that what runs good on Vendor A may run poor on Vendor B, or vice-versa).

Osbios
06-17-2014, 12:15 PM
The only real difference I know about is the buffer location. Meaning Vram or System Memory. (In case of APUs there is only System memory)
Modern drivers most likely also ignore your usage hint (GL_DYNAMIC_COPY) and decide on your usage patterns where to move/leave the buffer later on.