Jump to content
• Advertisement

Public Group

# concatenating Matrces for performance

This topic is 3011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I am reading and trying to better understand transformation in 3d space. Just curious to know if it would be better on performance Multiply scaling , rotation, and translation first before multiplying by the vertices of an object. Also, I would like to know if video games usually cache a full rotation for better performance.

#### Share this post

##### Share on other sites
Advertisement
I am not sure if I was clear, I meant, if it would be better to multiply the rotation, translation, scaling matrices together before multiplying an objects vertices.

#### Share this post

##### Share on other sites
Usually you don't have to transform verts by matricies much (on the CPU), but yeah, concatenating all your matrices into a final "transformation matrix" is usually the way to go.
e.g. Usually a basic vertex shader will just do:
output = input x modelViewProj
where modelViewProj is the result of concatenating together the "model to world", "world to view" and "projection" matrices in advance on the CPU. Doing this step once in advance saves doing it once for each vertex.
Quote:
 Also, I would like to know if video games usually cache a full rotation for better performance.
Do you mean, that each object would represent rotation in some other form, but then cache the result as a matrix?
If so, it can be more natural to use a matrix as the "natural" representation of rotation, and "cache" other formats (such as Euler) if they're even required.

#### Share this post

##### Share on other sites
Oddly not. Matrix-Vector operations are generally faster than Matrix-Matrix operations. Also, why are you storing translation as a Matrix? That's a huge waste of space:

Translation Matrix:
1 0 0 1 // Rotation0 1 0 1 // More Rotation0 0 1 1 // You get the feeling these three lines may not be necessary? :DX Y Z 1 // That fourth one is scaling. Er...Hmm...

Meh. Store your scaling and translation as Vectors and do a quick conversion any time you have to talk to DirectX/Your graphics engine. The time needed to convert, provided you code properly, is trivial compared with the time saved. Not to mention sanity...

Also, for rotation, just use a 3x3 RotationMatrix.

#### Share this post

##### Share on other sites
Lets say you have m many matrices and n many vertices, and the costs for a 4D matrix - vector product is C. A 4D matrix - matrix multiplication needs 4 times as many operations as a 4D matrix - vector product needs. So you have to compare
4 C ( m - 1 ) + C n ~ ( C n ) m
where C can be eliminated
4 m + n - 4 ~ n m
Checking m==1 gives n ~ n what seems okay.

Now, for m==2 you get 4 ~ n so that already 5 vertices are sufficient to let the costs of the non-composed method exceed those of the composed method in that case.

Collecting m on the left and n on the right gives you
4 ( m - 1 ) ~ n ( m - 1 ) == 4 ~ n
so that the relation is independent on m.

So, for any non-trivial case, composing transformations is better than applying the matrices one-by-one.

#### Share this post

##### Share on other sites
Ok, if I'm reading that right, you're saying that Matrix-Matrix multiplication is four times slower than Matrix-Vector multiplication, so it's better to do Matrix-Matrix multiplication than Matrix-Vector multiplication?

#### Share this post

##### Share on other sites
Quote:
 Ok, if I'm reading that right, you're saying that Matrix-Matrix multiplication is four times slower than Matrix-Vector multiplication, so it's better to do Matrix-Matrix multiplication than Matrix-Vector multiplication?

He's saying that multiplying a 4x4 and a 4x4 matrix takes more operations than multiplying 1x4 and 4x4 (or 4x4 and 4x1).

Quote:
 so it's better to do Matrix-Matrix multiplication than Matrix-Vector multiplication?

No, some times you don't have choice. However, if you do have a choice between doing three matrix-matrix multiplications plus one matrix-vector multiplication per iteration of some loop (such as one over all objects in your scene) or doing those three matrix-matrix out of the loop and doing them once, thus only performing a matrix-vector operation in the loop, then that is obviously vastly more efficient.

In practice of course, the world transform of every object differs and you can't pull all three matrix-matrix operations out in general. But you can still pull two of them out for improved efficiently.

He's saying that concatenating transforms together is generally more optimal. In fact, the earlier you can do so during scene processing, the better.

Quote:
 Also, I would like to know if video games usually cache a full rotation for better performance.

That depends very much on the game and what, precisely, you mean by 'cache.' It is almost never the case that the results of applying a transformation to vertices are cached, for example, as that is useless and inefficient. Similarly, base transformation properties (translation, rotation, scale) are usually not stored as matrices but rather turned into matrices as late as possible. This is more efficient in space -- although these days that matters much less, but then again most of the performance impacts of this discussion will be trivial -- but more importantly prevents you from accumulating floating point error bias into your transformation, which can result in undesired scaling or squishing of geometry transformed by said matrix.

#### Share this post

##### Share on other sites
...For some reason, my eyes skipped right over the "Vertices" part. So my advice is counter-productive for this situation.

#### Share this post

##### Share on other sites
Quote:
 Original post by Narf the MouseOk, if I'm reading that right, you're saying that Matrix-Matrix multiplication is four times slower than Matrix-Vector multiplication, so it's better to do Matrix-Matrix multiplication than Matrix-Vector multiplication?
Josh has answered already, but I want to clarify it:

I've shown that
( M1 * M2 ) * v
is more efficient than
M1 * ( M2 * v )
already for small numbers of vertices. Something like the above happens often, especially because of animations or object parenting (forward kinematics, skeletons).

Your objection was that a homogeneous transformation
M * v
is less efficient than its affine counterpart
t + R * v
(for position vectors) or
R * v
(for direction vectors) because the participants have one less dimension compared to the homogeneous version. This requires that the transformation is ready-to-use.

Both statements are okay.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
16
5. 5
• Advertisement

• 14
• 29
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002275
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!