From Mathias Froehlich, "This is a generic optimization that does not depend on any cpu or instruction

set.

The optimization is based on the observation that matrix matrix multiplication
with a dense matrix 4x4 is 4^3 Operations whereas multiplication with a
transform, or scale matrix is only 4^2 operations. Which is a gain of a
*FACTOR*4* for these special cases.
The change implements these special cases, provides a unit test for these
implementation and converts uses of the expensiver dense matrix matrix
routine with the specialized versions.

Depending on the transform nodes in the scenegraph this change gives a
noticable improovement.
For example the osgforest code using the MatrixTransform is about 20% slower
than the same codepath using the PositionAttitudeTransform instead of the
MatrixTransform with this patch applied.

If I remember right, the sse type optimizations did *not* provide a factor 4
improovement. Also these changes are totally independent of any cpu or
instruction set architecture. So I would prefer to have this current kind of
change instead of some hand coded and cpu dependent assembly stuff. If we
need that hand tuned stuff, these can go on top of this changes which must
provide than hand optimized additional variants for the specialized versions
to give a even better result in the end.

An other change included here is a change to rotation matrix from quaterion
code. There is a sqrt call which couold be optimized away. Since we divide in
effect by sqrt(length)*sqrt(length) which is just length ...
"
This commit is contained in:
Robert Osfield
2008-09-17 16:14:28 +00:00
parent 0598ac3b69
commit 22eae68e48
21 changed files with 723 additions and 193 deletions

View File

@@ -174,27 +174,12 @@ public:
private:
void rotate(float x, float y)
{
osg::Matrixd baseMatrix = _modelGroupTransform->getMatrix();
osg::Matrix baseMatrix = _modelGroupTransform->getMatrix();
osg::Matrixd preTransMatrix;
osg::Matrixd postTransMatrix;
osg::Matrixd rotMatrixX;
osg::Matrixd rotMatrixZ;
preTransMatrix.makeTranslate(_rotCenter);
postTransMatrix.makeTranslate(-_rotCenter);
rotMatrixZ.makeRotate((x - _prevX) * 3., osg::Vec3d(0.0, 0.0,1.0));
baseMatrix.preMult(preTransMatrix);
baseMatrix.preMult(rotMatrixZ);
baseMatrix.preMult(postTransMatrix);
rotMatrixX.makeRotate(-(y - _prevY) * 3., (baseMatrix * osg::Vec3d(1.0, 0.0,0.0)));
baseMatrix.preMult(preTransMatrix);
baseMatrix.preMult(rotMatrixX);
baseMatrix.preMult(postTransMatrix);
baseMatrix.preMultTranslate(_rotCenter);
baseMatrix.preMultRotate(osg::Quat((x - _prevX) * 3, osg::Vec3d(0.0, 0.0, 1.0)));
baseMatrix.preMultRotate(osg::Quat(-(y - _prevY) * 3, (baseMatrix * osg::Vec3d(1.0, 0.0, 0.0))));
baseMatrix.preMultTranslate(-_rotCenter);
_modelGroupTransform->setMatrix(baseMatrix);

View File

@@ -54,7 +54,7 @@ public:
if (cv)
{
osg::Vec3 eyePointLocal = cv->getEyeLocal();
matrix.preMult(osg::Matrix::translate(eyePointLocal.x(),eyePointLocal.y(),0.0f));
matrix.preMultTranslate(osg::Vec3(eyePointLocal.x(),eyePointLocal.y(),0.0f));
}
return true;
}
@@ -68,7 +68,7 @@ public:
if (cv)
{
osg::Vec3 eyePointLocal = cv->getEyeLocal();
matrix.postMult(osg::Matrix::translate(-eyePointLocal.x(),-eyePointLocal.y(),0.0f));
matrix.postMultTranslate(osg::Vec3(-eyePointLocal.x(),-eyePointLocal.y(),0.0f));
}
return true;
}

View File

@@ -67,11 +67,10 @@ class MyBillboardTransform : public osg::PositionAttitudeTransform
}
matrix.preMult(osg::Matrix::translate(-_pivotPoint)*
osg::Matrix::rotate(_attitude)*
osg::Matrix::rotate(billboardRotation)*
osg::Matrix::translate(_position));
matrix.preMultTranslate(_position);
matrix.preMultRotate(billboardRotation);
matrix.preMultRotate(_attitude);
matrix.preMultTranslate(_pivotPoint);
return true;
}

View File

@@ -146,7 +146,7 @@ public:
//osg::Matrixd matrix;
ellipsoid->computeLocalToWorldTransformFromLatLongHeight(_latitude,_longitude,_height,matrix);
matrix.preMult(osg::Matrix::rotate(_rotation));
matrix.preMultRotate(_rotation);
mt->setMatrix(matrix);
}

View File

@@ -19,6 +19,8 @@
#include "UnitTestFramework.h"
#include <osg/Matrixd>
#include <osg/Matrixf>
#include <osg/Vec3d>
#include <osg/Vec3>
#include <sstream>
@@ -84,4 +86,299 @@ OSGUTX_END_TESTSUITE
OSGUTX_AUTOREGISTER_TESTSUITE_AT(Vec3, root.osg)
///////////////////////////////////////////////////////////////////////////////
//
// Matrix Tests
//
class MatrixTestFixture
{
public:
MatrixTestFixture();
void testPreMultTranslate(const osgUtx::TestContext& ctx);
void testPostMultTranslate(const osgUtx::TestContext& ctx);
void testPreMultScale(const osgUtx::TestContext& ctx);
void testPostMultScale(const osgUtx::TestContext& ctx);
void testPreMultRotate(const osgUtx::TestContext& ctx);
void testPostMultRotate(const osgUtx::TestContext& ctx);
private:
// Some convenience variables for use in the tests
Matrixd _md;
Matrixf _mf;
Vec3d _v3d;
Vec3 _v3;
Quat _q1;
Quat _q2;
Quat _q3;
Quat _q4;
};
MatrixTestFixture::MatrixTestFixture():
_md(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
_mf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16),
_v3d(1, 2, 3),
_v3(1, 2, 3),
_q1(1, 0, 0, 0),
_q2(0, 1, 0, 0),
_q3(0, 0, 1, 0),
_q4(0, 0, 0, 1)
{
}
void MatrixTestFixture::testPreMultTranslate(const osgUtx::TestContext&)
{
osg::Matrixd tdo;
osg::Matrixd tdn;
osg::Matrixf tfo;
osg::Matrixf tfn;
tdo = _md;
tdn = _md;
tdo.preMult(osg::Matrixd::translate(_v3d));
tdn.preMultTranslate(_v3d);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.preMult(osg::Matrixd::translate(_v3));
tdn.preMultTranslate(_v3);
OSGUTX_TEST_F( tdo == tdn )
tfo = _mf;
tfn = _mf;
tfo.preMult(osg::Matrixf::translate(_v3d));
tfn.preMultTranslate(_v3d);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.preMult(osg::Matrixf::translate(_v3));
tfn.preMultTranslate(_v3);
OSGUTX_TEST_F( tfo == tfn )
}
void MatrixTestFixture::testPostMultTranslate(const osgUtx::TestContext&)
{
osg::Matrixd tdo;
osg::Matrixd tdn;
osg::Matrixf tfo;
osg::Matrixf tfn;
tdo = _md;
tdn = _md;
tdo.postMult(osg::Matrixd::translate(_v3d));
tdn.postMultTranslate(_v3d);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.postMult(osg::Matrixd::translate(_v3));
tdn.postMultTranslate(_v3);
OSGUTX_TEST_F( tdo == tdn )
tfo = _mf;
tfn = _mf;
tfo.postMult(osg::Matrixf::translate(_v3d));
tfn.postMultTranslate(_v3d);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.postMult(osg::Matrixf::translate(_v3));
tfn.postMultTranslate(_v3);
OSGUTX_TEST_F( tfo == tfn )
}
void MatrixTestFixture::testPreMultScale(const osgUtx::TestContext&)
{
osg::Matrixd tdo;
osg::Matrixd tdn;
osg::Matrixf tfo;
osg::Matrixf tfn;
tdo = _md;
tdn = _md;
tdo.preMult(osg::Matrixd::scale(_v3d));
tdn.preMultScale(_v3d);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.preMult(osg::Matrixd::scale(_v3));
tdn.preMultScale(_v3);
OSGUTX_TEST_F( tdo == tdn )
tfo = _mf;
tfn = _mf;
tfo.preMult(osg::Matrixf::scale(_v3d));
tfn.preMultScale(_v3d);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.preMult(osg::Matrixf::scale(_v3));
tfn.preMultScale(_v3);
OSGUTX_TEST_F( tfo == tfn )
}
void MatrixTestFixture::testPostMultScale(const osgUtx::TestContext&)
{
osg::Matrixd tdo;
osg::Matrixd tdn;
osg::Matrixf tfo;
osg::Matrixf tfn;
tdo = _md;
tdn = _md;
tdo.postMult(osg::Matrixd::scale(_v3d));
tdn.postMultScale(_v3d);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.postMult(osg::Matrixd::scale(_v3));
tdn.postMultScale(_v3);
OSGUTX_TEST_F( tdo == tdn )
tfo = _mf;
tfn = _mf;
tfo.postMult(osg::Matrixf::scale(_v3d));
tfn.postMultScale(_v3d);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.postMult(osg::Matrixf::scale(_v3));
tfn.postMultScale(_v3);
OSGUTX_TEST_F( tfo == tfn )
}
void MatrixTestFixture::testPreMultRotate(const osgUtx::TestContext&)
{
osg::Matrixd tdo;
osg::Matrixd tdn;
osg::Matrixf tfo;
osg::Matrixf tfn;
tdo = _md;
tdn = _md;
tdo.preMult(osg::Matrixd::rotate(_q1));
tdn.preMultRotate(_q1);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.preMult(osg::Matrixd::rotate(_q2));
tdn.preMultRotate(_q2);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.preMult(osg::Matrixd::rotate(_q3));
tdn.preMultRotate(_q3);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.preMult(osg::Matrixd::rotate(_q4));
tdn.preMultRotate(_q4);
OSGUTX_TEST_F( tdo == tdn )
tfo = _mf;
tfn = _mf;
tfo.preMult(osg::Matrixf::rotate(_q1));
tfn.preMultRotate(_q1);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.preMult(osg::Matrixf::rotate(_q2));
tfn.preMultRotate(_q2);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.preMult(osg::Matrixf::rotate(_q3));
tfn.preMultRotate(_q3);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.preMult(osg::Matrixf::rotate(_q4));
tfn.preMultRotate(_q4);
OSGUTX_TEST_F( tfo == tfn )
}
void MatrixTestFixture::testPostMultRotate(const osgUtx::TestContext&)
{
osg::Matrixd tdo;
osg::Matrixd tdn;
osg::Matrixf tfo;
osg::Matrixf tfn;
tdo = _md;
tdn = _md;
tdo.postMult(osg::Matrixd::rotate(_q1));
tdn.postMultRotate(_q1);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.postMult(osg::Matrixd::rotate(_q2));
tdn.postMultRotate(_q2);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.postMult(osg::Matrixd::rotate(_q3));
tdn.postMultRotate(_q3);
OSGUTX_TEST_F( tdo == tdn )
tdo = _md;
tdn = _md;
tdo.postMult(osg::Matrixd::rotate(_q4));
tdn.postMultRotate(_q4);
OSGUTX_TEST_F( tdo == tdn )
tfo = _mf;
tfn = _mf;
tfo.postMult(osg::Matrixf::rotate(_q1));
tfn.postMultRotate(_q1);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.postMult(osg::Matrixf::rotate(_q2));
tfn.postMultRotate(_q2);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.postMult(osg::Matrixf::rotate(_q3));
tfn.postMultRotate(_q3);
OSGUTX_TEST_F( tfo == tfn )
tfo = _mf;
tfn = _mf;
tfo.postMult(osg::Matrixf::rotate(_q4));
tfn.postMultRotate(_q4);
OSGUTX_TEST_F( tfo == tfn )
}
OSGUTX_BEGIN_TESTSUITE(Matrix)
OSGUTX_ADD_TESTCASE(MatrixTestFixture, testPreMultTranslate)
OSGUTX_ADD_TESTCASE(MatrixTestFixture, testPostMultTranslate)
OSGUTX_ADD_TESTCASE(MatrixTestFixture, testPreMultScale)
OSGUTX_ADD_TESTCASE(MatrixTestFixture, testPostMultScale)
OSGUTX_ADD_TESTCASE(MatrixTestFixture, testPreMultRotate)
OSGUTX_ADD_TESTCASE(MatrixTestFixture, testPostMultRotate)
OSGUTX_END_TESTSUITE
OSGUTX_AUTOREGISTER_TESTSUITE_AT(Matrix, root.osg)
}

View File

@@ -226,7 +226,7 @@ public:
if (cv)
{
osg::Vec3 eyePointLocal = cv->getEyeLocal();
matrix.preMult(osg::Matrix::translate(eyePointLocal));
matrix.preMultTranslate(eyePointLocal);
}
return true;
}
@@ -238,7 +238,7 @@ public:
if (cv)
{
osg::Vec3 eyePointLocal = cv->getEyeLocal();
matrix.postMult(osg::Matrix::translate(-eyePointLocal));
matrix.postMultTranslate(-eyePointLocal);
}
return true;
}