   concatenating Matrces for performance
  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.   
  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.   ;  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 modelViewProjwhere 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.   ;  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.   ;  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 compare4 C ( m - 1 ) + C n ~ ( C n ) mwhere C can be eliminated4 m + n - 4 ~ n mChecking 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 you4 ( m - 1 ) ~ n ( m - 1 ) == 4 ~ nso that the relation is independent on m.So, for any non-trivial case, composing transformations is better than applying the matrices one-by-one.   ;  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?   ;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.   ;  ...For some reason, my eyes skipped right over the "Vertices" part. So my advice is counter-productive for this situation.   ;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 ) * vis more efficient thanM1 * ( 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 transformationM * vis less efficient than its affine counterpartt + R * v(for position vectors) orR * 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.   