Followers 0

# OpenGL Vertex Array Object + Direct State Access

## 20 posts in this topic

As a disclaimer I am really not versed in OpenGL. I am trying to convert the following to use DSA:

glBindVertexArray(vao);

glBindBuffer(GL_ARRAY_BUFFER, vertex_attrib_buffers[0]);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

glBindBuffer(GL_ARRAY_BUFFER, vertex_attrib_buffers[1]);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);

glBindBuffer(GL_ARRAY_BUFFER, vertex_attrib_buffers[2]);
glVertexAttribPointer(2, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, NULL);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);


I have found the following functions:

glVertexArrayBindVertexBufferEXT()
glVertexArrayVertexAttribBindingEXT()
glVertexArrayVertexAttribFormatEXT()
glEnableVertexArrayAttribEXT()


but I am unsure of how to use them. I can find almost nothing about them online.

Edited by Chris_F
0

##### Share on other sites

These are described in the actual DSA extension: http://www.opengl.org/registry/specs/EXT/direct_state_access.txt

You can get arguments and their names from that site.

Yes, I read that. It is not complete. There is no mentioning at all of the first three functions I listed anywhere in that document.

0

##### Share on other sites

but I am unsure of how to use them.

Move back to VBO’s.

L. Spiro

0

##### Share on other sites

Keep in mind that when working with vertex array objects, there is one thing you cannot do with DSA and must use the old way: http://stackoverflow.com/questions/3776726/how-to-bind-a-element-buffer-array-to-vertex-array-object-using-direct-state-a

Wow, that is strange. I take it they accidentally left something like this out of the spec for some reason?

void glVertexArrayBindElementArrayBufferEXT(GLuint vao, GLuint buffer)


I guess it would be easy enough to implement on your own...

Move back to VBO’s.

OK, I'm confused now. I thought that VAOs were not optional in modern OpenGL. By "don't use VAOs," do you mean only use a single VAO and just keep modifying that one? Also, is it "vertex attribute object" or "vertex array object," or are those actually two distinct things?

Edited by Chris_F
0

##### Share on other sites

I'm confused as well.

I have 300 VBOs, give or take, that all have the same attributes. Hence why I chose VAOs because they remember my attribute bindings.

My reasoning is that I only end up with 2 calls to render something:

glBindVertexArray(vao);

glDraw...();

What is the correct course of action here?

Edited by Kaptein
0

##### Share on other sites

do you mean only use a single VAO and just keep modifying that one?

I definitely don’t mean that; that would be the worst-case scenario possible.

And they are not required as far as I know; the very fact that they appear in your list of functions as extensions means in any case you definitely are not strictly required to use them, and since they build on top of basic VBO functionality I doubt they will ever be strictly required.

Also, is it "vertex attribute object" or "vertex array object,"

Vertex Array Objects.

Hence why I chose VAOs because they remember my attribute bindings.

Or you could just set the attribute states once manually and render all of your VBO’s; that would be faster, especially if you are re-binding the VAO for each draw.

What is the correct course of action here?

Until VAO performance meets standards, the generally accepted course is to use raw VBO’s.
And if you emulate VAO functionality manually with a layer to remove redundant state changes (something you should be doing no matter what) a VAO will likely never be as fast as your manual setup calls.

L. Spiro Edited by L. Spiro
1

##### Share on other sites

I definitely don’t mean that; that would be the worst-case scenario possible.

And they are not required as far as I know;

I am even more confused now. Can you point me to an example of how to render with just VBOs and no VAO?

Edit: Are you talking about using functions like glVertexPointer in conjunction with VBOs? If so I should point out that all of that has been deprecated and is not even available in core profile as far as I know.

the very fact that they appear in your list of functions as extensions means in any case you definitely are not strictly required to use them, and since they build on top of basic VBO functionality I doubt they will ever be strictly required.

DSA is an extension, not VAOs. VAOs are core functionality.

Edited by Chris_F
0

##### Share on other sites
VAOs are certainly NOT a requirement for OpenGL... god, if it was, the horror... something you HAD to use with a bind-to-edit model... sweet jesus.

The commands you'd be after, based on the GL4.4 reference card, are those such as glVertexAttribPointer and friends ( http://www.opengl.org/wiki/Category:Core_API_Ref_Vertex_Arrays ) which allow you to setup buffer input via attributes on the shaders.
1

##### Share on other sites

VAOs are certainly NOT a requirement for OpenGL... god, if it was, the horror... something you HAD to use with a bind-to-edit model... sweet jesus.

The commands you'd be after, based on the GL4.4 reference card, are those such as glVertexAttribPointer and friends ( http://www.opengl.org/wiki/Category:Core_API_Ref_Vertex_Arrays ) which allow you to setup buffer input via attributes on the shaders.

Then how do you render in GL 3.3/4.x core profile without a VAO? I was under the impression that glVertexAttribPointer was only for VAOs.

EDIT: Just removed my VAO, bound my buffers, called glVertexAttribPointer and all I have is a black screen.

Edited by Chris_F
1

##### Share on other sites

I also tried using glVertexAttribPointer without VAOs and it simply didn't work.

How about a simple pseudo code example? :)

1

##### Share on other sites

To go one wiki page further, under glVertexAttribPointer: "GL_INVALID_OPERATION? is generated if no vertex array object is bound."

The following thread is also relevant, but nobody explicitly says anything like "yes, I've fallen back to older GLSL and non-generic attributes":

http://www.gamedev.net/topic/649916-valve-opengl-tips-and-tricks/

VAO is mandatory. Just not for people who are using compatibility mode with deprecated functionality apparently.

1

##### Share on other sites

ARB_vertex_attrib_binding also helps make VAOs make more sense - you can set up your formats, layouts, strides, etc in a VAO and then just bind the VAO and replace the buffers as required.  This is a better match to the common usage pattern of having the same vertex format but different buffers.

Unfortunately vertex attrib binding is not commonly supported either (thanks a lot, AMD and Intel).

The first 3 functions mentioned in the OP are part of ARB_vertex_attrib_binding and are documented with that extension.

2

##### Share on other sites

ARB_vertex_attrib_binding also helps make VAOs make more sense - you can set up your formats, layouts, strides, etc in a VAO and then just bind the VAO and replace the buffers as required.  This is a better match to the common usage pattern of having the same vertex format but different buffers.

Unfortunately vertex attrib binding is not commonly supported either (thanks a lot, AMD and Intel).

The first 3 functions mentioned in the OP are part of ARB_vertex_attrib_binding and are documented with that extension.

Luckily I'm targeting OpenGL 4.3+ so I have access to ARB_vertex_attrib_binding as a core feature.

0

##### Share on other sites
So, it turns out that you might want to be using VAOs after all....

http://www.openglsuperbible.com/2013/12/09/vertex-array-performance/

Which makes me wonder what Valve were doing to come to their conclusion, or maybe NV's Linux drivers just suck more than the Windows ones in this respect (NV benchmark pretty poorly in the above link too compared to AMD)
2

##### Share on other sites

The linux drivers for both AMD and Nvidia aren't as mature as their windows counterparts. It might be that it's just a collection of performance tips relating specially to linux. There is no viable alternative to VAOs though, which is why we are all so confused. You can't skip VAOs because you often have 300 VBOs with the same attribs, and attribs aren't remembered for VBOs, or anything else, except VAOs.

Note: I have had better performance on Linux overall though!

OpenGL is a bit ...-backwards sometimes.

You ideally just "enable" your 5 attribs, and then proceed to render 300 VBOs. You can't. It sucks.

Instead I have to create 300 VAOs, with 5 "enabled" attribs each. Then render 300 VAOs.

The madness stops at GL 4.x (from superbible):

"However, these functions were introduced in OpenGL 4.3 and so may not have widespread support just yet, whereas VAO has been in core OpenGL since version 3.0"

What were they thinking?

Edited by Kaptein
1

##### Share on other sites

So, it turns out that you might want to be using VAOs after all....

http://www.openglsuperbible.com/2013/12/09/vertex-array-performance/

Which makes me wonder what Valve were doing to come to their conclusion, or maybe NV's Linux drivers just suck more than the Windows ones in this respect (NV benchmark pretty poorly in the above link too compared to AMD)

I would like to see his benchmarks when he is not redundantly calling glEnableVertexAttribArray() and glDisableVertexAttribArray().

L. Spiro

0

##### Share on other sites

There is no viable alternative to VAOs though, which is why we are all so confused.

Cross-platform, no. But on NVidia, bindless can easily exceed the performance you get from VAOs, and the reason is intuitive.

You ideally just "enable" your 5 attribs, and then proceed to render 300 VBOs. You can't. It sucks.

Instead I have to create 300 VAOs, with 5 "enabled" attribs each. Then render 300 VAOs.

No you don't. Having a bazillion little VAOs floating around with all the cache misses that go with them isn't necessarily the best approach. Best case, use bindless (does an end-run around the cache issues, but is NV-only) or a streaming VBO approach with reuse (which keeps the bind count down, and works cross-platform).

If you have a number of separate, preloaded VBOs, you absolutely can't/refuse to make them large enough to keep you from being CPU bound for some reason, and you can't/won't use bindless, then fall back on (in the order of the performance I've observed on NVidia) 1) VBOs with VAOs, one per batch state combination, 2) client arrays, or 3) VBOs without VAOs or with one VAO for all.

In case it's not obvious, I do what gives me the best performance. I'm not a core purist.

Its really unfortunate AMD hasn't stepped up and supported bindless in their OpenGL drivers, at least for launching batches (vertex attribute and index list specification).
1

##### Share on other sites

I unfortunately don't have that luxury. The world is fully 3D and i employ a large number of optimizations..

If there was any way at all to get even 20 ns i would rewrite everything in a heartbeat. The only caveat is that it has to work on any opengl 3.x implementation.

Also 300 VBOs was just a number. It's more like 300 to 5000, depending on config.ini settings.

Ideally I could draw 5000 per frame at 60 fps. But that wouldn't be right, now would it.

It's usually around 2000, going down to 1000 after occlusion culling. (A frame behind)

The terrain is dynamic and is produced on the fly. When the player moves, so the world "revolves." Trying to combine VBOs is definitely possible. I just wonder if it won't severely affect the GPU in the end. Combining all terrain data of the same (sectoral) x,z (internally known as columns) is something I'm already doing. The world just isn't a plane.

Edited by Kaptein
0

## Create an account

Register a new account

Followers 0

• ### Similar Content

• Hello, I have been working on SH Irradiance map rendering, and I have been using a GLSL pixel shader to render SH irradiance to 2D irradiance maps for my static objects. I already have it working with 9 3D textures so far for the first 9 SH functions.
In my GLSL shader, I have to send in 9 SH Coefficient 3D Texures that use RGBA8 as a pixel format. RGB being used for the coefficients for red, green, and blue, and the A for checking if the voxel is in use (for the 3D texture solidification shader to prevent bleeding).
My problem is, I want to knock this number of textures down to something like 4 or 5. Getting even lower would be a godsend. This is because I eventually plan on adding more SH Coefficient 3D Textures for other parts of the game map (such as inside rooms, as opposed to the outside), to circumvent irradiance probe bleeding between rooms separated by walls. I don't want to reach the 32 texture limit too soon. Also, I figure that it would be a LOT faster.
Is there a way I could, say, store 2 sets of SH Coefficients for 2 SH functions inside a texture with RGBA16 pixels? If so, how would I extract them from inside GLSL? Let me know if you have any suggestions ^^.
• By DaniDesu
#include "MyEngine.h" int main() { MyEngine myEngine; myEngine.run(); return 0; } MyEngine.h
#pragma once #include "MyWindow.h" #include "MyShaders.h" #include "MyShapes.h" class MyEngine { private: GLFWwindow * myWindowHandle; MyWindow * myWindow; public: MyEngine(); ~MyEngine(); void run(); }; MyEngine.cpp
#pragma once #include <glad\glad.h> #include <GLFW\glfw3.h> class MyWindow { private: GLFWwindow * windowHandle; int windowWidth; int windowHeight; const char * windowTitle; public: MyWindow(int windowWidth, int windowHeight, const char * windowTitle); ~MyWindow(); GLFWwindow * getWindowHandle(); void createWindow(); void MyWindow::destroyWindow(); }; MyWindow.cpp
#include "MyWindow.h" MyWindow::MyWindow(int windowWidth, int windowHeight, const char * windowTitle) { this->windowHandle = NULL; this->windowWidth = windowWidth; this->windowWidth = windowWidth; this->windowHeight = windowHeight; this->windowTitle = windowTitle; glfwInit(); } MyWindow::~MyWindow() { } GLFWwindow * MyWindow::getWindowHandle() { return this->windowHandle; } void MyWindow::createWindow() { // Use OpenGL 3.3 and GLSL 3.3 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Limit backwards compatibility glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Prevent resizing window glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Create window this->windowHandle = glfwCreateWindow(this->windowWidth, this->windowHeight, this->windowTitle, NULL, NULL); glfwMakeContextCurrent(this->windowHandle); } void MyWindow::destroyWindow() { glfwTerminate(); } MyShapes.h
#pragma once #include <glad\glad.h> #include <GLFW\glfw3.h> class MyShapes { public: MyShapes(); ~MyShapes(); GLuint & drawTriangle(float coordinates[]); }; MyShapes.cpp
#include "MyShapes.h" MyShapes::MyShapes() { } MyShapes::~MyShapes() { } GLuint & MyShapes::drawTriangle(float coordinates[]) { GLuint vertexBufferObject{}; GLuint vertexArrayObject{}; // Create a VAO glGenVertexArrays(1, &vertexArrayObject); glBindVertexArray(vertexArrayObject); // Send vertices to the GPU glGenBuffers(1, &vertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW); // Dertermine the interpretation of the array buffer glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *)0); glEnableVertexAttribArray(0); // Unbind the buffers glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); return vertexArrayObject; } MyFileHandler.h
#pragma once #include <cstdio> #include <cstdlib> class MyFileHandler { private: const char * fileName; unsigned long fileSize; void setFileSize(); public: MyFileHandler(const char * fileName); ~MyFileHandler(); unsigned long getFileSize(); const char * readFile(); }; MyFileHandler.cpp
#include "MyFileHandler.h" MyFileHandler::MyFileHandler(const char * fileName) { this->fileName = fileName; this->setFileSize(); } MyFileHandler::~MyFileHandler() { } void MyFileHandler::setFileSize() { FILE * fileHandle = NULL; fopen_s(&fileHandle, this->fileName, "rb"); fseek(fileHandle, 0L, SEEK_END); this->fileSize = ftell(fileHandle); rewind(fileHandle); fclose(fileHandle); return; } unsigned long MyFileHandler::getFileSize() { return (this->fileSize); } const char * MyFileHandler::readFile() { char * buffer = (char *)malloc((this->fileSize)+1); FILE * fileHandle = NULL; fopen_s(&fileHandle, this->fileName, "rb"); fread(buffer, this->fileSize, sizeof(char), fileHandle); fclose(fileHandle); buffer[this->fileSize] = '\0'; return buffer; } VertexShader.glsl
#version 330 core layout (location = 0) vec3 VertexPositions; void main() { gl_Position = vec4(VertexPositions, 1.0f); } FragmentShader.glsl
#version 330 core out vec4 FragmentColor; void main() { FragmentColor = vec4(1.0f, 0.0f, 0.0f, 1.0f); } I am attempting to create a simple engine/graphics utility using some object-oriented paradigms. My first goal is to get some output from my engine, namely, a simple red triangle.
For this goal, the MyShapes class will be responsible for defining shapes such as triangles, polygons etc. Currently, there is only a drawTriangle() method implemented, because I first wanted to see whether it works or not before attempting to code other shape drawing methods.
The constructor of the MyEngine class creates a GLFW window (GLAD is also initialized here to load all OpenGL functionality), and the myEngine.run() method in Main.cpp is responsible for firing up the engine. In this run() method, the shaders get loaded from files via the help of my FileHandler class. The vertices for the triangle are processed by the myShapes.drawTriangle() method where a vertex array object, a vertex buffer object and vertrex attributes are set for this purpose.
The while loop in the run() method should be outputting me the desired red triangle, but all I get is a grey window area. Why?
(Note: I am aware that this code is not using any good software engineering practices (e.g. exceptions, error handling). I am planning to implement them later, once I get the hang of OpenGL.)

• By KarimIO
EDIT: I thought this was restricted to Attribute-Created GL contexts, but it isn't, so I rewrote the post.
Hey guys, whenever I call SwapBuffers(hDC), I get a crash, and I get a "Too many posts were made to a semaphore." from Windows as I call SwapBuffers. What could be the cause of this?
Update: No crash occurs if I don't draw, just clear and swap.
static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 32, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 24, // 24Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; if (!(hDC = GetDC(windowHandle))) return false; unsigned int PixelFormat; if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) return false; if (!SetPixelFormat(hDC, PixelFormat, &pfd)) return false; hRC = wglCreateContext(hDC); if (!hRC) { std::cout << "wglCreateContext Failed!\n"; return false; } if (wglMakeCurrent(hDC, hRC) == NULL) { std::cout << "Make Context Current Second Failed!\n"; return false; } ... // OGL Buffer Initialization glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glBindVertexArray(vao); glUseProgram(myprogram); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, (void *)indexStart); SwapBuffers(GetDC(window_handle));
• By Tchom
Hey devs!

I've been working on a OpenGL ES 2.0 android engine and I have begun implementing some simple (point) lighting. I had something fairly simple working, so I tried to get fancy and added color-tinting light. And it works great... with only one or two lights. Any more than that, the application drops about 15 frames per light added (my ideal is at least 4 or 5). I know implementing lighting is expensive, I just didn't think it was that expensive. I'm fairly new to the world of OpenGL and GLSL, so there is a good chance I've written some crappy shader code. If anyone had any feedback or tips on how I can optimize this code, please let me know.

uniform mat4 u_MVPMatrix; uniform mat4 u_MVMatrix; attribute vec4 a_Position; attribute vec3 a_Normal; attribute vec2 a_TexCoordinate; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { v_Position = vec3(u_MVMatrix * a_Position); v_TexCoordinate = a_TexCoordinate; v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0)); gl_Position = u_MVPMatrix * a_Position; } Fragment Shader
precision mediump float; uniform vec4 u_LightPos["+numLights+"]; uniform vec4 u_LightColours["+numLights+"]; uniform float u_LightPower["+numLights+"]; uniform sampler2D u_Texture; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { gl_FragColor = (texture2D(u_Texture, v_TexCoordinate)); float diffuse = 0.0; vec4 colourSum = vec4(1.0); for (int i = 0; i < "+numLights+"; i++) { vec3 toPointLight = vec3(u_LightPos[i]); float distance = length(toPointLight - v_Position); vec3 lightVector = normalize(toPointLight - v_Position); float diffuseDiff = 0.0; // The diffuse difference contributed from current light diffuseDiff = max(dot(v_Normal, lightVector), 0.0); diffuseDiff = diffuseDiff * (1.0 / (1.0 + ((1.0-u_LightPower[i])* distance * distance))); //Determine attenuatio diffuse += diffuseDiff; gl_FragColor.rgb *= vec3(1.0) / ((vec3(1.0) + ((vec3(1.0) - vec3(u_LightColours[i]))*diffuseDiff))); //The expensive part } diffuse += 0.1; //Add ambient light gl_FragColor.rgb *= diffuse; } Am I making any rookie mistakes? Or am I just being unrealistic about what I can do? Thanks in advance
• By yahiko00
Hi,
Not sure to post at the right place, if not, please forgive me...
For a game project I am working on, I would like to implement a 2D starfield as a background.
I do not want to deal with static tiles, since I plan to slowly animate the starfield. So, I am trying to figure out how to generate a random starfield for the entire map.
I feel that using a uniform distribution for the stars will not do the trick. Instead I would like something similar to the screenshot below, taken from the game Star Wars: Empire At War (all credits to Lucasfilm, Disney, and so on...).

Is there someone who could have an idea of a distribution which could result in such a starfield?
Any insight would be appreciated

• 28
• 12
• 11
• 31
• 20