2

A few closely-related questions regarding buffer objects in OpenGL.

  1. Besides persistent mapping, is there any other reason to allocate an immutable buffer? Even if the user allocates memory for the buffer only once, with mutable buffers he always has the ability to do it again if he needs to. Plus, with mutable buffers you can explicitly specify a usage hint.

  2. How do people usually change data through a mapped pointer? The way I see it, you can either make changes to a single element, or multiple. For single-element changes all I could think of is an operator[] on a mapped pointer as if it was a C-style array. For multi-element changes, only thing I could think of is a memcpy, but in that case isn't it just better to use glBufferSubData?

  3. Speaking of glBufferSubData, is there truly any difference between calling it and just doing a memcpy on a mapped pointer? I've heard the former does more than 1 memcpy, is it true?
  4. Is there a known reason why you can't specify a usage hint for an immutable buffer?

I know these questions are mostly about performance, and thus can be answered with a simple "just do some profiling and see", but at the time of me asking this it's not so much about performance as it is about design - i.e., I want to know the good practices of choosing between a mutable buffer vs an immutable one, and how should I be modifying their contents.

BDL
  • 21,052
  • 22
  • 49
  • 55
  • It's really hard to answer all of your questions. 2 is probably too opinion based because it asks how people do things. About 3) [Related](https://stackoverflow.com/questions/32222574/is-it-better-glbuffersubdata-or-gl), [Also Related](https://stackoverflow.com/questions/44667194/when-does-glbuffersubdata-return) – BDL Jul 23 '19 at 12:42

1 Answers1

3

Even if the user allocates memory for the buffer only once, with mutable buffers he always has the ability to do it again if he needs to.

And that's precisely why you shouldn't use them. Reallocating a buffer object's storage (outside of invalidation) is not a useful thing. Drivers have to do a lot of work to make it feasible.

So having an API that takes away tools you shouldn't use is a good thing.

How do people usually change data through a mapped pointer?

You generally use whatever tool is most appropriate to the circumstance. The point of having a mapped pointer is to access the storage directly, so writing your data elsewhere and copying it in manually is kind of working against that purpose.

Is there a known reason why you can't specify a usage hint for an immutable buffer?

Because the immutable buffer API was written by people who didn't want to have terrible, useless, and pointless parameters. The usage hint on mutable buffers is completely ignored by several implementations because users were so consistently confused about what those hints mean that people used them in strange scenarios.

Immutable buffers instead make you state how you intend to use the buffer, and then hold you to it. If you ask for a static buffer whose contents you will never modify, then you cannot modify it, period. This is prevented at the API level, unlike usage hints, where you could use any buffer in any particular way regardless of the hint.

Hints were a bad idea and needed to die.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982