09/17/08 18:14:28 (9 years ago)

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

The optimization is based on the observation that matrix matrix multiplication
with a dense matrix 4x4 is 43 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 ...

1 modified


  • OpenSceneGraph/trunk/src/osg/AutoTransform.cpp

    r8804 r8868  
    8484bool AutoTransform::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const 
     86    if (_scale.x() == 0.0 || _scale.y() == 0.0 || _scale.z() == 0.0) 
     87        return false; 
    8689    if (_referenceFrame==RELATIVE_RF) 
    8790    { 
    88         matrix.postMult(osg::Matrix::translate(-_position)* 
    89                         osg::Matrix::rotate(_rotation.inverse())* 
    90                         osg::Matrix::scale(1.0/_scale.x(),1.0/_scale.y(),1.0/_scale.z())* 
    91                         osg::Matrix::translate(_pivotPoint)); 
     91        matrix.postMultTranslate(-_position); 
     92        matrix.postMultRotate(_rotation.inverse()); 
     93        matrix.postMultScale(Vec3d(1.0/_scale.x(), 1.0/_scale.y(), 1.0/_scale.z())); 
     94        matrix.postMultTranslate(_pivotPoint); 
    9295    } 
    9396    else // absolute 
    9497    { 
    95         matrix = osg::Matrix::translate(-_position)* 
    96                  osg::Matrix::rotate(_rotation.inverse())* 
    97                  osg::Matrix::scale(1.0/_scale.x(),1.0/_scale.y(),1.0/_scale.z())* 
    98                  osg::Matrix::translate(_pivotPoint); 
     98        matrix.makeRotate(_rotation.inverse()); 
     99        matrix.preMultTranslate(-_position); 
     100        matrix.postMultScale(Vec3d(1.0/_scale.x(), 1.0/_scale.y(), 1.0/_scale.z())); 
     101        matrix.postMultTranslate(_pivotPoint); 
    99102    } 
    100103    return true; 
    105108    if (!_matrixDirty) return; 
    107     _cachedMatrix.set(osg::Matrix::translate(-_pivotPoint)* 
    108                       osg::Matrix::scale(_scale)* 
    109                       osg::Matrix::rotate(_rotation)* 
    110                       osg::Matrix::translate(_position)); 
     110    _cachedMatrix.makeRotate(_rotation); 
     111    _cachedMatrix.postMultTranslate(_position); 
     112    _cachedMatrix.preMultScale(_scale); 
     113    _cachedMatrix.preMultTranslate(-_pivotPoint); 
    112115    _matrixDirty = false;