PDA

View Full Version : switch GL_DYNAMIC_DRAW to GL_STATIC_DRAW at runtime?



JasonRay
01-22-2016, 06:54 AM
I created a buffer using:

glBufferData(GL_TEXTURE_BUFFER, size, 0, GL_DYNAMIC_DRAW);

can I switch the 'usage' of this buffer to GL_STATIC_DRAW at runtime?
If yes, how?

carsten neumann
01-22-2016, 08:10 AM
You cannot change the usage hint of a buffer object. That being said, since these are just hints the implementations tend to ignore them and instead look at how your code actually uses the buffer object to determine which memory pool to place the buffer in.
In contrast the flags passed to glBufferStorage are not just hints, they are promises that certain operations (e.g. reads/writes) will not occur on the buffer object.
At least on Nvidia drivers on a debug context with GL_DEBUG_OUTPUT enabled the implementation tells you where it allocates a buffer object or when it decides to migrate one between memory pools.

JasonRay
01-22-2016, 08:23 AM
Thank you for your helpful reply.

My application shows the OpenGL debug output, which is:

---------------------opengl-callback-start------------
message: Buffer detailed info: Buffer object 511 (bound to GL_TEXTURE_BUFFER, usage hint is GL_DYNAMIC_DRAW) has been mapped WRITE_ONLY in SYSTEM HEAP memory (fast).
type: OTHER
id: 131185
severity:
---------------------opengl-callback-end--------------

I was given the information that GL_DYNAMIC_DRAW would slow down accessing the buffer, as it uses the SYSTEM HEAP.
Is SYSTEM HEAP slow? What's SYSTEM HEAP at all, is it in the normal RAM, or is it in the video card's VRAM?

Is there a way to speed up the buffer access in this case? The buffer is written to some few times and mostly read.

mhagain
01-22-2016, 09:04 AM
Is there a way to speed up the buffer access in this case? The buffer is written to some few times and mostly read.

In the following I am assuming that by "mostly read" what you really mean is that the buffer is mostly used for drawing; i.e it's the GPU that's reading the buffer, not your own program code. If it is the latter (i.e your own program is doing the reading) then there is no fast case and your best approach is to duplicate the data in system memory via a malloc call.

I would create the buffer with GL_STATIC_DRAW.

Best case: the driver honours the hint, you get the buffer created in memory that's fast for drawing from, and the occcasional write doesn't trip up the driver's heuristics by causing it to relocate the buffer to slower memory.

Worst case: the buffer relocates to slower memory and you're no worse off than you currently are.

JasonRay
01-22-2016, 09:22 AM
I'll try out GL_STATIC_DRAW... thank you both for your help!