Jump to content

  • Log In with Google      Sign In   
  • Create Account


Shaders and VBOs, Performance and Relation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 19 March 2013 - 02:17 PM

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.


Edited by Danicco, 19 March 2013 - 02:19 PM.


Sponsor:

#2 wintertime   Members   -  Reputation: 1640

Like
2Likes
Like

Posted 19 March 2013 - 02:48 PM

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.



#3 mhagain   Crossbones+   -  Reputation: 7592

Like
2Likes
Like

Posted 19 March 2013 - 03:07 PM

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/


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 JoeJ   Members   -  Reputation: 199

Like
0Likes
Like

Posted 19 March 2013 - 03:33 PM

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.



#5 mhagain   Crossbones+   -  Reputation: 7592

Like
0Likes
Like

Posted 19 March 2013 - 03:39 PM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#6 JoeJ   Members   -  Reputation: 199

Like
0Likes
Like

Posted 19 March 2013 - 04:34 PM

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...



#7 noizex   Members   -  Reputation: 853

Like
0Likes
Like

Posted 19 March 2013 - 06:34 PM

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.


Edited by noizex, 19 March 2013 - 06:34 PM.


#8 JoeJ   Members   -  Reputation: 199

Like
-1Likes
Like

Posted 20 March 2013 - 02:09 AM

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.



#9 noizex   Members   -  Reputation: 853

Like
0Likes
Like

Posted 20 March 2013 - 03:19 AM

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. 


Edited by noizex, 20 March 2013 - 03:25 AM.


#10 JoeJ   Members   -  Reputation: 199

Like
1Likes
Like

Posted 20 March 2013 - 04:16 AM

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.



#11 noizex   Members   -  Reputation: 853

Like
0Likes
Like

Posted 20 March 2013 - 04:34 AM

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.


Solely for complete nonsense regarding FPS measurement. I don't downvote as response to an attack or anything.

 

As to both methods, I agree there shouldn't be any meaningful difference, which is why I'm pointing that measuring by FPS gives you completly wrong sense of performance. So it would be wise to start by picking correct measuring method, and based on that trying to find bottleneck or just ignoring this (which I'd do, seeing such a tiny difference in miliseconds between two methods). 



#12 Danicco   Members   -  Reputation: 449

Like
0Likes
Like

Posted 22 March 2013 - 05:55 PM

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.

 

In the second example I use the basic glLoadIdentity()glTranslatef() and glRotate() as in the last code of block with my camera object to get where I'm looking at. In the 2nd example I don't think there's any perspective projection calculation in my draw function, other than the glutPerspective() call in my initialization.

 

My FPS for a teapot was at about 2500ish with the 2nd option and deprecated calls, and it dropped to 2000~2100ish with the new matrices calculations. I'm going to do what @wintertime's suggested and calculate the time per function so I get better measurements.

 

This site explains a bit of the advantages of using matrices (Q4), though I didn't really understand the last part as I still don't get matrices at all: "However, while this may seem extravagant, the savings come from processing each vertex. Using matrix multiplication, the savings made from processing just 4 vertices, will outweigh the additional set-up cost." (I think I understand the concept, but I don't quite see it in my code yet).

 

This is what I think it's going on my code:



void Game::Draw()
{
    glLoadIdentity();
    glTranslatef(); //go to camera target (center)
    glRotatef(); //rotate according to the camera view in X
    glRotatef(); //rotate according to the camera view in Y
    glTranslatef(); //go to the camera's position (eye)

    //loop to draw all objects
    //I just draw the object, each of their vertexes' xyz is already defined
}

I think of this as 5 matrix calculations, and then I just draw the objects, compared to the 1st option I mentioned:



mat4 mProjection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
void Game::Draw()
{
    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;

    //loop to draw all objects
    //each vertex is multiplied by mMVP
}

This looks like at least 3 matrix calculations, but it's calculated for every single vertex of the objects, since I send it to the shader calculate the final xyz of each vertex by multiplying it for the MVP I sent.

 

It looks there's much more overhead in the 2nd example, but I can't be sure as I don't know what's happening when I draw after calling the (deprecated) glMatrixFunctions(), and this is what I'm asking in this thread.

Since it's deprecated now, I think it means my assumptions about being heavier are wrong, or the performance loss is negligible, but I'd like to understand what's going on.

 

It's that programmer "I need to understand what's happening!" thing, I think...


Edited by Danicco, 22 March 2013 - 05:56 PM.


#13 Ameise   Members   -  Reputation: 691

Like
0Likes
Like

Posted 05 April 2013 - 10:13 AM

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

 

Since when is glVertexAttribPointer deprecated?






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS