<p>Rarely, if ever. You half-answered it in your own question: a vertex shader runs once per-vertex, a fragment shader once per-fragment. If you're not doing something that's unique to that vertex or fragment, then you're doing literally the exact same thing every time you invoke a shader. That doesn't sound more efficient to me.</p>

CC BY-SA 3.0
2015-08-28T04:03:06.527Z
2015-08-28T04:03:06.527Z
Mokosha
<p>Doing math with uniforms is a shader won't usually get you any performance over doing it on the CPU. A CPU isn't slower than a GPU at doing matrix math, it just isn't structured so as to do large amounts of math in parallel. But you have to actually do that large amount of math to get a win. Sending extra data to the GPU just to have the GPU multiply two matrices together will rarely net you anything.</p>

<p>Now, say you have a buffer of skinning matrices. It <em>might</em> start to matter whether you transform them all into world space on the CPU or just pass an extra model-to-world-space matrix to the GPU. But even then it depends on your ratio of vertices to bones.</p>

CC BY-SA 3.0
2015-09-03T09:30:42.943Z
I depends on the amount of calculations.GPU is really fast about math operations.So it is a matter of try and benchmarking.
Michael IV
CC BY-SA 3.0
2015-08-26T05:11:33.353Z
2015-08-26T05:11:33.353Z
John Calsbeek
<p>As of now, when rendering my scene and while iterating through the scene graph, for each node its model matrix is calculated from the parent's model matrix and the scene node's pose. This is quite simple and already reduces the number of matrix multiplications to one multiplication per node and frame. </p>

<p>But all these matrix multiplications have to be done on the CPU and in every frame to be able to perform (multiple consecutive) movements of scene nodes very fast. The GPU, however, is probably much better suited to perform lots of matrix multiplications, so I'm thinking about sending multiple partial model matrices to the vertex shader instead of computing everything on the CPU.</p>

<p>Sending every single part (so the pose of every node) to the GPU probably does not make much sense, as in this case all the computations are done for every vertex instead of every node, which would actually decrease performance. But maybe scene nodes with lots of children or only non-moving children (relative to its parent) could be a place to split the model matrix and shift the multiplications to the shader.</p>

<p>So, when is it better to send partial model matrices to the shader and move the multiplication to the GPU? Or is it simply a bad idea to to this?</p>

<opengl><performance><transformations><scene-graph>
When is it better to upload partial model matrices to the vertex shader?
Nero
# When is it better to upload partial model matrices to the vertex shader?

(Asked by: username_0 on 2015-08-25)

As of now, when rendering my scene and while iterating through the scene graph, for each node its model matrix is calculated from the parent's model matrix and the scene node's pose. This is quite simple and already reduces the number of matrix multiplications to one multiplication per node and frame.

But all these matrix multiplications have to be done on the CPU and in every frame to be able to perform (multiple consecutive) movements of scene nodes very fast. The GPU, however, is probably much better suited to perform lots of matrix multiplications, so I'm thinking about sending multiple partial model matrices to the vertex shader instead of computing everything on the CPU.

Sending every single part (so the pose of every node) to the GPU probably does not make much sense, as in this case all the computations are done for every vertex instead of every node, which would actually decrease performance. But maybe scene nodes with lots of children or only non-moving children (relative to its parent) could be a place to split the model matrix and shift the multiplications to the shader.

So, when is it better to send partial model matrices to the shader and move the multiplication to the GPU? Or is it simply a bad idea to to this?

**Tags:** opengl, performance, transformations, scene-graph


---

## 2 answers

### Answer by username_2 on 2015-08-26. Score: 9 | Accepted answer

Doing math with uniforms is a shader won't usually get you any performance over doing it on the CPU. A CPU isn't slower than a GPU at doing matrix math, it just isn't structured so as to do large amounts of math in parallel. But you have to actually do that large amount of math to get a win. Sending extra data to the GPU just to have the GPU multiply two matrices together will rarely net you anything.

Now, say you have a buffer of skinning matrices. It *might* start to matter whether you transform them all into world space on the CPU or just pass an extra model-to-world-space matrix to the GPU. But even then it depends on your ratio of vertices to bones.

> **Comments:**
> **username_3**: I depends on the amount of calculations.GPU is really fast about math operations.So it is a matter of try and benchmarking.  

---

### Answer by username_1 on 2015-08-28. Score: 5

Rarely, if ever. You half-answered it in your own question: a vertex shader runs once per-vertex, a fragment shader once per-fragment. If you're not doing something that's unique to that vertex or fragment, then you're doing literally the exact same thing every time you invoke a shader. That doesn't sound more efficient to me.


---

