openglmemory-managementmemory-fragmentation

Does OpenGL takes care of GPU memory fragmentation?


So basically whenever I create buffer objects Opengl allocates some memory on the GPU.

Consider scenario 1 where I generate 2 uniform buffers for 2 uniform variables.

Now consider scenario 2 where I create a single buffer and enclose the 2 uniform variables inside an interface block.

My understanding is that for scenario 1, two separate regions of memory get allocate while for scenario 2, one big contiguous block of memory gets allocated. If so, then Scenario 1 might be susceptible to memory fragmentation and if this happens is it managed by OpenGL or something else OR should we keep this in mind before writing performance critical code?


Solution

  • Actually I have to fix that for you. It's

    So basically whenever I create buffer objects Opengl allocates some memory.

    You don't know – and it's invalid to make assumptions – about the whereabouts of where this memory is located. You just get the assurance that it's there (somewhere) and that you can make use of it.

    managed by OpenGL or something

    Yes. In fact, and reasonable OpenGL implementations do have to move around data on a regular basis. Think about it: On a modern computer system several applications do use the GPU in parallel, and neither process (usually) does care about or respect the inner working of the other processes that coinhabit the same machine. Yet the user (naturally) expects, that all processes will "just work" independent of the situation.

    The GPU drivers do a lot of data pushing in the background, moving stuff between the system memory, the GPU memory or even swap space on storage devices without processes noticing any of that.

    OR should we keep this in mind before writing performance critical code?

    Average-joe-programmer will get the best performance by just using the OpenGL API in a straightforward way, without trying to outsmart the implementation. Every OpenGL implementation (= combination of GPU model + driver version) has "fast paths", however short of having access to intimately detailed knowledge about the GPU and driver details those are very difficult to hit.

    Usually only the GPU makers themselves have this knowledge; if you're a AAA game studio, you're usually having a few GPU vendor guys on quick dial to come for a visit to your office and do their voodoo; most people visiting this site probably don't.