Shaders and VBOs, Performance and Relation

Started by
11 comments, last by Ameise 11 years ago

I'm following some new tutorials about OpenGL, since the ones I had done before were way outdated (like 2000ish), and I have some questions about the Shader x VBO relation.

My vertex shader code is this:




#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 MVP;

void main()
{
	//option 1
	vec4 v = vec4(vertexPosition_modelspace, 1);
	gl_Position = MVP * v;

	//option 2
	gl_Position.xyz = vertexPosition_modelspace;
	gl_Position.w = 1.0;
}

(Only one of the options is enabled, I just put both here to save some space)

And the drawing code is this:




glBindBuffer(GL_ARRAY_BUFFER, object->_VertexID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object->_VertexIndexID);
		
glEnableClientState(GL_VERTEX_ARRAY);
	
//glVertexPointer(3, GL_FLOAT, 0, 0); //I was using this originally
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

glDrawElements(GL_TRIANGLES, object->GetTotalIndexes(), GL_UNSIGNED_INT, 0);
		
glDisableClientState(GL_VERTEX_ARRAY);

Most VBOs tutorials I find use the glVertexPointer() function, but it seems that when I have more than one array of values in the shader (like vertices, colors and normals), I need to use glVertexAttribPointer() instead, and match the first value with the location = X in the shader code.

I didn't understand very well what's this relation about, does this mean the values currently bound in the C++ code are sent to the layout(location) variable in my shader code?

Also, I've read a bunch of tutorials about OpenGL and topics about it, and I see plenty of them talking about matrix calculations and such, but I never bothered to understand what they meant. I had always reached the results I desired without them, but seeing I'm finding more and more about them in newer tutorials, I'm curious. For example, there's a tutorial that shows how to calculate the "view" of each object with matrices (and GLM):




mat4 mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
mat4 mView = glm::lookAt(
	glm::vec3(0, 0, 0), //Camera Position
	glm::vec3(0, 0, 0), //Looking to Position
	glm::vec3(0, 1, 0) //Head Up
	);
mat4 mModel = glm::mat4(1.0f); //model values
mat4 mMVP = mProjection * mView * mModel;

Then I send the mMVP to the shader, and using the option1 in the shader, I get the correct view for the object(s) on screen.

However, I was achieving the same doing this:




glLoadIdentity();
glTranslatef(-_camera.targetX, -_camera.targetY, -_camera.targetZ); //Looking to Position
glRotatef(_camera.RotationX(), 1.0f, 0.0f, 0.0f); //0-360 values for rotations
glRotatef(_camera.RotationY(), 0.0f, 1.0f, 0.0f);
glTranslatef(-_camera.posX, -_camera.posY, -_camera.posZ); //camera position

//Draw Objects

And with this, I used the option2 in the shader.

On my machine, option2 was about 25% faster.

But I don't know if the shader calculations are done by the graphics card (meaning my processor was faster at calculating them) or not (then it's the reverse).

Another option I was considering was not calculating the matrices in the C++ code, but just send them to the shader and let it calculate the final options, but I'm guessing this would be even slower.

I've read that I shouldn't use FPS as a "performance" check because it's inconstant, but I don't know any other way to compare methods and figure which is better.

So, what's the generally better accepted method?

If you have arguments on why should one use matrices instead of the glFunctions (although I've read that glRotatef() is being marked as deprecated), I'd love to hear it as well. Most tutorials and books don't say which is better, it's usually "do this and you get the results you want".

I plan on developing games for mobiles so I'm really concerned about performance and also for curiosity, I'd really like to understand this better.

Advertisement

Yes the opengl matrix stack is deprecated, but it should do the same if you use it the same way as glm. You could even use glm to calculate the matrix and then do a glLoadMatrix() (just to try it out or if you would have been stuck with an old opengl version).

FPS is no good measurement because it is nonlinear and upside down, only meaningful between maybe 10 and 60 and it by definition measures a whole frame and not single calculations. You better use milliseconds or microseconds per frame or function.

Regarding your first question, you don't need the gl(Enable|Disable)ClientState calls when using attrib arrays. It's an artefact of the evolution of OpenGL that there are two ways of specifying geometry via vertex arrays or VBOs; the old way uses fixed attributes (via glVertexPointer, glTexCoordPointer, etc), the newer uses generic attributes (via glVertexAttribPointer). With the old way you're restricted to predefined attribute types (position, normal, texcoord, etc), each of which may have their own further restrictions, and you have to make glClientActiveTexture calls if you want to use more than one set of texcoords - basically it's ugly and inflexible. All new code should use generic attributes instead.

You're correct about the "layout(location=0)" qualifier on your shader input - the location specified here maps to the first parameter of glEnableVertexAttribArray and glVertexAttribPointer. This is a relatively new feature (not that new though - all reasonably civilized drivers should support it) and is exposed by the GL_ARB_explicit_attrib_location extension; if you don't have that then the GLSL compiler will assign it's own locations and you'll need to do a bunch of glGetAttribLocation calls to find out what they are (or glBindAttribLocation to set them to values you want).

It's an unfortunate fact that much OpenGL tutorial material is woefully outdated and will teach you bad practices. For a more modern view this set of tutorials is widely acknowledged to be quite good: http://www.arcsynthesis.org/gltut/

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

On the performance difference:

Did you check that while using option 1 both model space and projection are initialized to identity?

If not, an additianal (useless) matrix mult after the shader could explain the difference.

I guess if you load identity matrix by numbers OGL does not recognize that you want to turn them off,

but if you call glLoadIdentity() it should and performance should be equal.

On the performance difference:

Did you check that while using option 1 both model space and projection are initialized to identity?

If not, an additianal (useless) matrix mult after the shader could explain the difference.

I guess if you load identity matrix by numbers OGL does not recognize that you want to turn them off,

but if you call glLoadIdentity() it should and performance should be equal.

This was certainly true back in the old days (the "Performance tips" section of the Red Book even had an entry for it: "Use glLoadIdentity to initialize a matrix, rather than loading your own copy of the identity matrix.") but I highly doubt if it's relevant today - particularly if sending a matrix via glUniformMatrix.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

This was certainly true back in the old days

Maybe that's because i'm a 2000ish guy starting modern OGL too :D

And while learning new stuff i become very unsure if someone says the old stuff is faster.

I'd really like to know the reason for the performance difference...

First off, these two options are not equal, at least in terms of the source code you pasted. First one also calculates perspective projection and lookat - both which can be calculated lazily and only when certain parameters change. If you do this every frame then its one thing thats wrong. In second example you don't even mention lookat or perspective matrix - where are these calculations?

The way you measure it is also wrong, as you said you're using frames/s and you should be using ms/frame.

Using proper matrices and sending them as uniforms (or UBO) is the right way. Plus, as you mentioned, use of glRotate and similar calls is deprecated, and thats perfect reason not to use that kind of code.


Where are we and when are we and who are we?
How many people in how many places at how many times?

Don't agree to all of that.

Both options calculate a combined modelview / projection matrix once per frame.

I assume Danicco simply forgot to post the 2nd projection setup, and matrix setup does not affect performance because he uses lots of vertices (?)

And both should do a single matrix mult per vertex.

Option 1 does it in shader and option 2 too, because OGL will add the necessary instructions automatically (If it doesn't know it should not), right?

Thus my assumption that this accidently happens to option 1 too.

Maybe there's even a driver bug forcing this to happen all the time - who knows?

Also, pure FPS should be accurate enough to measure a 25% difference, if there are enough vertices in the test.

Don't agree to all of that.

Both options calculate a combined modelview / projection matrix once per frame.

I assume Danicco simply forgot to post the 2nd projection setup, and matrix setup does not affect performance because he uses lots of vertices (?)

And both should do a single matrix mult per vertex.

Option 1 does it in shader and option 2 too, because OGL will add the necessary instructions automatically (If it doesn't know it should not), right?

Thus my assumption that this accidently happens to option 1 too.

Maybe there's even a driver bug forcing this to happen all the time - who knows?

Also, pure FPS should be accurate enough to measure a 25% difference, if there are enough vertices in the test.

To be honest I haven't addressed his performance issues because we know nothing about how he measured this and how everything else works. There are so many things that can affect the results that its IMO pointless with such little information about that particular case.

What is relevant is that he shouldn't be using deprecated functionality (glRotate, glEnableClientState when using glVertexAttribPointer).

Also, I have no idea what vertices number has anything to do with FPS vs ms/frame measurment. Lets assume (because he never wrote what are his numbers even) that he measures FPS and don't have any v-sync and anything that would limit how many frames are drawn, so he draws as many as possible in a single frame. I also assume that he doesn't do anything else in his test case so we can say that faster method is around 3000 FPS probably:

2 option (deprecated, potentially faster):

3000 FPS = 0.33 ms/frame

1 option (using glm matrices, said to be 25% slower)

2250 FPS = 0.44 ms/frame
(3000*0.75)

So this gives you difference of 0.11ms per frame in "slow" case. This means that your insanely big 25% difference in FPS in this case is merely 0.11ms difference, which is not that much anymore? From here you can start measuring what exactly happens that causes this slowdown.

Of course his FPS count can be different, as wintertime mentioned, measuring with FPS makes a bit more sense below 60FPS level, but its still better to use ms/frame there.


Where are we and when are we and who are we?
How many people in how many places at how many times?

Downgrade? Don't treat my disagreement as attack to you or the things you said.

I just wanted to point out that both methods should result in equal GPU instructions and performance.

Except the performence difference all the other questions already had good answers.

We all know we should not use deprecated stuff and how to put a percent value in relation with a fixed number.

This topic is closed to new replies.

Advertisement