2

Functions such as glGenBuffers and glGenVertexArrays have an n parameter that allows for multiple indexes names to be generated.

My question is: What is the advantage of generating multiple indexes names at once versus generating them 1 at a time? Is there one?


Background: I'm working on constructing an object-oriented framework but I don't know whether or not I should add functionality for index name wizardry (at the cost of more complex interfaces) or just auto-generate individual indexes names as needed per object.

Aberrant
  • 3,423
  • 1
  • 27
  • 38
  • You could have something like a pool of VAO's then a pool of buffer objects per vertex array. Only allocate the buffer objects when a VAO is referenced for the first time and you will have very little cost. – RamblingMad Aug 03 '15 at 17:35
  • @CoffeeandCode I suppose that makes sense, although I think that reason is ignorable in my particular case. I think this qualifies as an answer, by the way. – Aberrant Aug 03 '15 at 17:39
  • It would be faster than allocating a new vertex array every time you need one. – RamblingMad Aug 03 '15 at 17:39
  • @CoffeeandCode Certainly, but for my case I expect very little allocation and a lot of drawing and context switching. I was mainly worried that individually allocated indexes would perform slowly compared to batch-allocated ones in some way. As an answer to my question you are definitely right though. – Aberrant Aug 03 '15 at 17:45

2 Answers2

4

In practice it makes no difference. If you look at the part of the API that deals with shaders, you'll see, that this does return only one index at a time. In fact a lot of experienced OpenGL coders prefer to wrap the glGen… functions in myglCreate… functions that return exactly one name of the desired object class.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
1

I completely agree with @datenwolf's recommendation, but wanted to provide some more reasoning. I would only call glGen*() for multiple names if it's the most convenient thing to do. If you already have an array of names anyway, and want to create them all at once, then go ahead and use a single call. But it's definitely not worth jumping through any hoops, and I can't imagine a realistic scenario where it would help performance.

The glGen*() calls only create object names (which is the official OpenGL terminology, even though many people call them "ids"), not objects. The actual object is only created the first time you bind a name. So there's no possible gain from creating multiple objects at once, the only (insignificant) gain is from generating multiple names at once.

I can think of two related reasons why generating the names isn't much more than a drop in the ocean:

  • It doesn't happen at a high frequency. What you should typically worry about are calls that are made very frequently, like state update calls that are made between draw calls, and the draw calls themselves.
    Name generation typically happens at a moderate frequency during setup, and rarely later. Maybe you'll create a new buffer or texture every once in a while, but this is mostly insignificant compared to how often you bind textures or buffers.
  • It is tied to much more expensive operations. You only generate as many names as you need for creating the actual objects. And creating the object is much more expensive than generating the name. If you picture everything that needs to happen for creating a typical object (various memory allocations, state setup, etc), generating the name is an insignificant part.
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133