• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Danicco

Shaders and VBOs, Performance and Relation

12 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites

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/

2

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

-1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

 

Since when is glVertexAttribPointer deprecated?

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0