Jump to content

  • Log In with Google      Sign In   
  • Create Account


Custom view matrices reduces FPS phenomenally


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
17 replies to this topic

#1 TRONJon   Members   -  Reputation: 189

Like
0Likes
Like

Posted 19 April 2014 - 01:15 PM

So I've been upgrading my engine to use custom view matrices instead of the OpenGL gl_ModelView and gl_Projection which are deprecated in newer versions.

 

Now, as I'm using Shadow mapping, Skeletal Animation, and various other shader techniques, I've split my matrices into:

modelMatrix

viewMatrix

modelViewMatrix

projectionMatrix

modelViewProjectionMatrix

 

So each time I translate() an object, or manipulate any of these matrices, all 5 are uploaded to my Uniform Buffer Object on the GPU.

 

And my FPS has dropped from 1200 to 170, this is unacceptable for me considering all I've done is change the matrices behind the scene. Nothing has changed in the engine itself.

 

Can someone tell me what has caused the drop in performance? I'm guessing it's something along the lines of:

- My matrix operations in Java are slow

- Uploading 5 matrices regularly is using up my bandwidth?



Sponsor:

#2 mhagain   Crossbones+   -  Reputation: 7435

Like
0Likes
Like

Posted 19 April 2014 - 02:20 PM

- My matrix operations in Java are slow

- Uploading 5 matrices regularly is using up my bandwidth?

 

Probably neither.  Unless you've got a really really bad matrix library, or an absolutely huge amount of matrices to upload, both of which are extreme and unlikely scenarios, you'll need to look elsewhere.

 

That UBO update - that's what I'd point my finger at.  There are threads about UBO performance and how slow they are to update, as well as the hoops you need to jump through in order to make them fast again.

 

Before we go any further this is worth testing and fortunately it's an extremely simple and minimally-intrustive test.  Just convert to standalone uniforms from a UBO.  See if things improve.  If they do then we've established that yes, it's the UBO that's causing your performance problems.


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.


#3 CoffeeandCode   Members   -  Reputation: 172

Like
-1Likes
Like

Posted 19 April 2014 - 05:13 PM

So each time I translate() an object, or manipulate any of these matrices, all 5 are uploaded to my Uniform Buffer Object on the GPU.


You can't be doing this, its too expensive.

If you modify a model matrix, don't update your projection matrix just for shits and giggles, that's inefficient and a huge performance drop. Imagine how many times per second you are doing that!

Instead, figure out the offset of each matrix into the buffer and store those indices then whenever you NEED to do an operation, map the buffer and modify the matrix based on one of those indices.

Also, instead of working out the model view proj matrix on the CPU, do the multiplication in your shader. GPUs are far better at matrix multiplication in almost any situation.

#4 Hodgman   Moderators   -  Reputation: 27647

Like
0Likes
Like

Posted 19 April 2014 - 05:14 PM

Instead of looking at it as an FPS drop, look at it as an increase of 5ms of work per frame. Put some timers in your code and try and find where this work has been added.

#5 powly k   Members   -  Reputation: 631

Like
1Likes
Like

Posted 19 April 2014 - 05:20 PM

Upload them only once just before rendering - though the driver will probably already do this for you behind the scenes.

 

 

The more important point you might want to consider is how you measure your performance. If you get >1000fps with those various effects, your scene is probably too small to test on. If your rendering is not the bottleneck, then your memory lanes are. So you could probably throw a way more complex scene at the program and it'd run at the same speed. Another point is that measuring with fps can be deluding - a drop from 1200fps to 170fps is not that massive, the render times went from 1ms to 6ms - you should maybe measure which parts take how much time, opengl has time query objects for this.



#6 mhagain   Crossbones+   -  Reputation: 7435

Like
2Likes
Like

Posted 19 April 2014 - 05:21 PM

Also, instead of working out the model view proj matrix on the CPU, do the multiplication in your shader. GPUs are far better at matrix multiplication in almost any situation.

 

...but the CPU typically only has to do this particular multiplication once, whereas the GPU will need to do it per vertex.  Yes, the GPU is faster, but tens of thousands of times per frame versus once?  It's not that much faster.


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.


#7 TheChubu   Crossbones+   -  Reputation: 3705

Like
1Likes
Like

Posted 19 April 2014 - 05:22 PM


If you modify a model matrix, don't update your projection matrix just for shits and giggles, that's inefficient and a huge performance drop. Imagine how many times per second you are doing that!
This.

 

You don't update the uniforms each time you modify a matrix.

 

First you do all your computations (rotations, translations, scaling, model view projection, whatever) for all your objects. Then, when you're about to draw the mesh, you update the uniforms for that object.


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#8 mhagain   Crossbones+   -  Reputation: 7435

Like
0Likes
Like

Posted 19 April 2014 - 05:27 PM

Upload them only once just before rendering - though the driver will probably already do this for you behind the scenes.

 

 

The more important point you might want to consider is how you measure your performance. If you get >1000fps with those various effects, your scene is probably too small to test on. If your rendering is not the bottleneck, then your memory lanes are. So you could probably throw a way more complex scene at the program and it'd run at the same speed. Another point is that measuring with fps can be deluding - a drop from 1200fps to 170fps is not that massive, the render times went from 1ms to 6ms - you should maybe measure which parts take how much time, opengl has time query objects for this.

 

I'd counter-argue that going from 1ms to 6ms is extremely significant, particularly if all other factors are equal between the two tests.  You've just blown one-third of your frametime budget on ... nothing.  Yes, that's significant.

 

Now, if it was going from - say - 8ms to 13 ms, you'd have a point, particularly if there was a nice new effect, higher LOD, or whatever to look at in return for it.  Blowing one-third of your frametime budget just on account of using a different way of doing the same thing?  Nope, you don't have a point, sorry.


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.


#9 CoffeeandCode   Members   -  Reputation: 172

Like
-4Likes
Like

Posted 19 April 2014 - 05:40 PM

whereas the GPU will need to do it per vertex.


If your graphics drivers are any good, it will only do the multiplication once. If your driver can't perform this optimization, switch gpu vendor.

#10 Hodgman   Moderators   -  Reputation: 27647

Like
2Likes
Like

Posted 19 April 2014 - 06:12 PM

whereas the GPU will need to do it per vertex.

If your graphics drivers are any good, it will only do the multiplication once. If your driver can't perform this optimization, switch gpu vendor.
I'd love to see proof of this. In my experience, if you ask the GPU to perform operations on uniforms per vertex/pixel, then the GPU will do so. The only "preshaders" that I've seen that are reliable are ones that modify your shader code ahead of time and generate x86 routines for patching your uniforms...
Anyway, even if this does work on 1 vendor, you're just playing into the hands of their marketing department by deliberately writing bad code that's going to (rightfully) run slow for two thirds of your users, and act as a marketing tool for one vendor :(

#11 mhagain   Crossbones+   -  Reputation: 7435

Like
0Likes
Like

Posted 19 April 2014 - 06:42 PM

 

whereas the GPU will need to do it per vertex.


If your graphics drivers are any good, it will only do the multiplication once. If your driver can't perform this optimization, switch gpu vendor.

 

 

Who's going to tell that to your users after you release Turbo Wombat IV and it sells 20 million copies, but runs slow for 10 million of them?  You?


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.


#12 Aks9   Members   -  Reputation: 770

Like
1Likes
Like

Posted 21 April 2014 - 04:42 AM

If you modify a model matrix, don't update your projection matrix just for shits and giggles, that's inefficient and a huge performance drop. Imagine how many times per second you are doing that!

 

When we are speaking of optimization, NV does not transfer values to uniforms if they are not changed. It is probably not the case for buffers. Of course, buffers are transfered to graphics card memory only before they are actually used. So, frequent change before drawing should not affect performance significantly. Especially because it is a small amount of data in case of uniform blocks and calls communicate only with drivers' memory space in main memory.

 

 

Also, instead of working out the model view proj matrix on the CPU, do the multiplication in your shader. GPUs are far better at matrix multiplication in almost any situation.

 

 

I have strongly to disagree with this statement. Model/view/projection matrix calculation is far better to be done on the CPU side. In case of scientific visualization, when precision is important, CPU (when say this I mean Intel, because I'm not familiar with AMD architecture) can generate 10 orders of magnitude more precise matrices than GPU. I don't even know how such huge number is called. :) Transformations cumulatively generates errors. If double precision is not used the transformation cannot be accurate enough. Further more, transcendental functions  are calculated only using single precision on the GPU. CUDA and similar APIs emulate double precision for such functions, but in OpenGL there is no transcendental functions emulations. I agree that hardware implemented transcendental functions are enormously fast. No CPU can compete with GPUs in that field. Just a single clock interval for a function call! Besides the fact that the number of SFU (as they are called) are not equal to SP units, pipeline usually hides the latency imposed by waiting for the SFU. But, as I already said, the high-level accuracy cannot be achieved.



#13 clb   Members   -  Reputation: 1777

Like
0Likes
Like

Posted 21 April 2014 - 05:45 AM

As far as I have understood it, Uniform Buffer Objects were created exactly for the need of bulk-updating multiple uniforms in a single call. Submitting a single UBO that contains mere five matrices should be a trivial workload. Refactoring that to multiple UBOs e.g. where one would contain model matrices and the other would contain projection matrices like was suggested above sounds like a heavy antioptimization - don't do that! (unless profiling suggests that two UBO uploads are faster than one in this case :o)

 

Or perhaps the discussion has confused the use of uniforms with a call to glUniformMatrix4fv without UBOs, and UBOs themselves. If you are not using UBOs and are manually updating uniform matrices with glUniformMatrix4fv, the there is benefit in optimizing to not redundantly change matrices that haven't changed.

 

Hodgman's suggestion is the sanest here:

   - Stop measuring FPS, but instead start measuring milliseconds. This will give better sense of the actual difference in workload.

   - Use a CPU profiler with the old code and the new code to compare where the extra added time is being spent. E.g. AMD CodeAnalyst is good (works on non-AMD CPUs as well). If it turns out to not be a CPU-side slowdown (the profiles are identical), then use e.g. nVidia Parallel Studio or AMD CodeXL to debug and profile the GPU side operation.


Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#14 mhagain   Crossbones+   -  Reputation: 7435

Like
2Likes
Like

Posted 21 April 2014 - 06:52 AM

I can pretty much guarantee where the slowdown is.  It's not in the matrix multiplication, it's not in binding UBOs to the pipeline.  The OP is doing a separate UBO update for each object drawn.  That's potentially tens, hundreds or thousands of UBO updates per frame.

 

The slowdown is in GL's buffer object API, because you just can't make this kind of high-frequency update and still maintain performance when using it.  Any profiling is just going to show a huge amount of time in the driver waiting for buffer object API calls to finish, waiting on CPU/GPU synchronization, and waiting on GL client/server synchronization.

 

The solution is to not use small UBOs and to not update per object.  Instead you create a single UBO large enough to hold all objects, figure out the data that needs updating ahead of time, do one single big UBO update per frame (preferably via glBufferSubData), then a bunch of glBindBufferRange calls per-object.  That runs fast, and in the absence of persistent mapping it's the only way to get performance out of UBOs.


Edited by mhagain, 21 April 2014 - 06:53 AM.

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.


#15 L. Spiro   Crossbones+   -  Reputation: 12232

Like
2Likes
Like

Posted 21 April 2014 - 09:27 AM

Also, instead of working out the model view proj matrix on the CPU, do the multiplication in your shader.

Never perform matrix multiplication in a shader. All matrices that will be used in the shader should already be precomputed on the CPU.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#16 Alundra   Members   -  Reputation: 770

Like
0Likes
Like

Posted 21 April 2014 - 10:34 AM


 

Also, instead of working out the model view proj matrix on the CPU, do the multiplication in your shader.

Never perform matrix multiplication in a shader. All matrices that will be used in the shader should already be precomputed on the CPU.


L. Spiro

 

 

Except for skinning, but I agree with L. Spiro because you have to have in mind this mul will be done on each vertex or each pixel.


Edited by Alundra, 21 April 2014 - 10:35 AM.


#17 Hodgman   Moderators   -  Reputation: 27647

Like
2Likes
Like

Posted 21 April 2014 - 10:03 PM

There are cases where it can be a good idea to keep view-projection and world matrices separate. Say you've got 10k static objects, if merging these transforms, the CPU has to perform 10k world*viewProj operations, and upload the 10k resultant matrices every frame. If kept separate, the CPU only has to upload the new viewProj matrix, and doesn't have to change any per-object data at all (but of course the GPU now has to do the 10k*numVerts matrix concatenations instead).
The "right" decision depends entirely on the game (and target hardware).

#18 JohnnyCode   Members   -  Reputation: 184

Like
0Likes
Like

Posted 22 April 2014 - 04:19 PM

 

upgrading my engine to use custom view matrices instead of the OpenGL gl_ModelView and gl_Projection which are

were you setting any other uniforms in the old deprecated scenario?

How many uniform writes do you do per frame? roughly (batch complexity)






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