Draw multiple buffers with the same vertex layout - opengl

I have multiple objects, each one of them has its own index buffer, vertex buffer, some have a different shader or texture, and all of them have the same vertex format (x, y, z, u, v, nx, ny, nz). I don't want to batch my objects together, but render them in separate draw calls. Lets say that I want to render 5 different objects (cubes, spheres, etc.), do I have to create a new vao for each one of them, or is there a way to tell OpenGL that I want to render 5 different buffers with the same layout/format?

You don't need to tell OpenGL this; you simply modify the VAO and change the buffers without changing the vertex format.
Now granted, you can't do that without providing the vertex format parameters to glVertexAttribPointer. So at the very least, you have to remember what those parameters still are.
Fortunately, separate attribute format exists (in GL 4.3+), which allows you to change the buffer bindings (which are still stored in a VAO) separately from the functions that change the vertex format. So you should just be able to call glBindVertexBuffer and glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ...) to change the buffers.

Related

What is the best way to draw multiple VAO Using the same shader but not having the same texture or colors

I'm wondering what would be the best thing to do if I want to draw
more than ~6000 different VAOs using the same shader.
At the moment I bind my shader then give it all information needed (uniform) then looping through each VAO to binding and draw them.
This code make my computer fall at ~ 200 fps instead of 3000 or 4000.
According to https://learnopengl.com/Advanced-OpenGL/Instancing, using glDrawElementsInstanced can allow me to handle a HUGE amount of same VAO but since I have ~6000 different VAO It seems like I can't use it.
Can someone confirm me this? What you guys would do to draw so many VAO and save many performance as you can?
Step 1: do not have 6,000 different VAOs.
You are undoubtedly treating each VAO as a separate mesh. Stop doing this. You should instead treat each VAO as a separate vertex format. That is, you only need a new VAO if you're passing different kinds of vertex data. The number of attributes and the format of each attributes constitute the format information.
Ideally, you only need between 4 and 10 separate sets of vertex formats. Given that you're using the same shader on multiple VAOs, you probably already have this understanding.
So, how do you use the same VAO for multiple meshes? Ideally, you would do this by putting all of the mesh data for a particular kind of mesh (ie: vertex format) in the same buffer object(s). You would select which data to retrieve for a particular rendering operation via tricks like the baseVertex parameter of glDrawElementsBaseVertex, or just by selecting which range of index data to draw from for a particular draw command. Other alternatives include the multi-draw family of rendering functions.
If you cannot put all of the data in the same buffers for some reason, then you should adopt the glVertexAttribFormat style of VAO usage. That way, you set your vertex format data with glVertexAttribFormat calls, and you can change the buffers as needed with glBindVertexBuffers without ever having to touch the vertex format itself. This is known to be faster than changing VAOs.
And to be honest, you should adopt glVertexAttribFormat anyway, because it's a much better API that isn't stupid like glVertexAttribPointer and its ilk.
glDrawElementsInstanced can allow me to handle a HUGE amount of same VAO but since I have ~6000 differents VAO It seems like I can't use it.
So what you should do is to combine your objects into the same VAO. Then use glMultiDrawArraysIndirect or glMultiDrawElementsIndirect to issue a draw of all the different objects from within the same VAO. This answer demonstrates how to do this.
In order to handle different textures you either build a texture atlas, pack the textures into a texture array, or use the GL_ARB_bindless_texture extensions if available.

Can I set VAO before VBO in OpenGL?

I have a Sphere class that generate the VBO for creating given input radius and some more parameters.
Each Sphere VBO share the same memory layout (let's say vertex indice 0 = vertices, 1 = colors).
I may getting wrong, but if a understand correctly, state is what VAO store. But I don't know if VAO reminds which VBO was bound or use the currently bound VBO. But I think it use the VBO bound when modifying it (so it imply that reconfiguring the VAO for each Sphere render)
Question #1
Is it possible to store on VAO for all of the spheres? Sharing one VAO for multiple VBO.
Question #2
Can we set up the VAO independently from the VBO ? I wanna say, before even creating the VBO and without VBO bound, for example in a static function before even creating Spheres.
Question #3
This question may have non sens and be case-specific, but should I use one VAO and one VBO for each Sphere or share one VAO for all the Spheres (if it's possible) ?
Using the separate attribute format API, it is easy to set up a VAO independently from any particular set of source buffer objects. The vertex format information (component type, count, etc) can all be established via glVertexAttribFormat without a buffer object. You can even set up the relationship between attribute indices and buffer binding points, so that you can read interleaved attributes from a single buffer binding. All without ever binding a buffer object.
Now at the end of the data, you will have to attach buffer objects to the VAO when it comes time to render (via glBindVertexBuffer(s)). And the VAO will store the buffers it was last set with. But you should essentially ignore this. Treat a VAO as if it were just the vertex format state, with buffer binding being something that you do right before you render with it.
And yes, having one VAO and multiple potential buffers that get read with that vertex format (or better yet, just one buffer with multiple mesh data in it that all share the same vertex format, with mesh selection being done via the baseVertex parameter of glDrawElementsBaseVertex) is the right way to go.
All that being said, you also should remember that all spheres are the same mesh. They may appear in different locations with different sizes, but that's just a matter of providing a different transform of a unit sphere. The only reason to use more than one sphere mesh is if you need different resolutions of spheres (more polygons vs. fewer).

What is the purpose of VAO in OpenGL

I'm starting to learn modern OpenGL, and as the title says, I just wanted to be sure of the purpose of VAO's in the rendering pipeline.
When rendering we use VBO to store datas, and then we use OpenGL functions like: glAttribe to say to the GPU that we are going to use this datas "in That way", like: the first 3 floats in the vertices that we passes through vbo are in fact positions, and the next 3 floats are colors etc... So then I readed that we need some VAO that stores the descriptions of the vertices but what's the goal there ?
Thanks in advance.
Vertex array objects store a set of buffer names (usually vertex and index buffers) to get vertex data from, as well as how the vertices are layed out in the vertex buffers.
Their main purpose is so that when you want to render a different model from a different set of buffers, instead of binding each buffer and then setting the vertex attribute formats each time, you just bind a different VAO, and all the buffers and attributes are set up for you.
Not only is this more convenient for the programmer, it reduces the number of OpenGL calls required and thus CPU usage, which can clear up a CPU bottleneck.

OpenGL - Is it possible to use texture buffers with interleaved VBOs?

Is there a method for binding only a single attribute of a VBO to a texture buffer object?
Problem is, I use an interleaved VBO for drawing spheres with the attributes position, radius and another one. Now, I want to use another shader which draws other stuff using instanced geometry at the positions in my sphere VBO.
What I could do is just texelFetch() the data I need which is really ugly concerning the needed alignment (VBO attributes have different size) and the unused space, which is bad because of the limited size of a texture buffer. Another way could be copying only the data I need from one VBO to a separate VBO, but that's not really satisfying either.
So... is there another possibility?
Buffer textures do not have attributes. So what you're talking about doesn't make sense.
Also, VBOs are not a thing; there is no such thing as a "VBO". There are simply buffer objects and different uses for them. glVertexAttribPointer does not modify the buffer object. It just tells OpenGL's vertex reading system how to fetch data from it. Other systems that fetch data from buffer objects use their own mechanisms for doing so, which are entirely separate.
Like buffer textures.
If you want to "draws other stuff using instanced geometry", why don't you just use instancing? Apply a divisor (presumably of 1) to those attributes, so that they will only get different values for different instances.

VBO per shader attribute without VAO, or different approach?

I'm using various 'nested' contexts in my application (for shader and geometry sharing), so I cannot use VAOs as they cannot be shared across contexts.
My mesh data is stored in one VBO as a 'pool' of unique vertices, and in another VBO I store the indices that make up the faces. I'm currently using this successfully for wireframe rendering in my CAD app, but I would like to add a flat shaded mode - and for that I need face normals.
If I needed vertex normals, I would just append the data to the vertex position data, but I can't do that here because a vertex would have a different face normal depending on which face it belonged to is being rendered. Ideally I would like to create another VBO pair holding my face normals and indices, however I can't bind two VBOs to the same target without a VAO - even though they're used in different attributes.
I appreciate that the normal route is to use a VAO which does let you assign multiple VBOs to the same target, but I can't use them because my geometry context is shared and VAOs are not. Are there any alternative solutions to this issue?
If I needed vertex normals, I would just append the data to the vertex position data, but I can't do that here because a vertex would have a different face normal depending on which face it belonged to is being rendered. Ideally I would like to create another VBO pair holding my face normals and indices, however I can't bind two VBOs to the same target without a VAO - even though they're used in different attributes.
Your problem has nothing to do with VAOs or buffer objects. VAOs do not allow you to have multiple index lists. They do not allow you to have "face normals".
A vertex array objects is nothing more than a container that stores vertex attribute bindings. They do not enable you to do anything you couldn't do without them; there's simply a convenient way to change all attribute bindings.
You need to break your vertex positions up into different faces. So you need to duplicate position values, so that each unique position/normal pair is properly unique. VAOs don't get around this limitation.
FYI: the reason VAOs aren't shared is because they're too simple to bother sharing. You can set the same VAO up in two different contexts easily enough. Just create a VAO in the context you want to use it in, and do what you would normally do to set it up. It's just a simple state object; it doesn't really contain anything.