BGE: Use float as default instead of double in Moto library.
authorPorteries Tristan <republicthunderbolt9@gmail.com>
Sun, 13 Dec 2015 01:01:28 +0000 (02:01 +0100)
committerJorge Bernal <jbernalmartinez@gmail.com>
Sun, 13 Dec 2015 01:09:05 +0000 (02:09 +0100)
Use float in moto instead of double for MT_Scalar.
This switch allow future optimization like SSE.
Additionally, it changes the OpenGL calls to float versions as they are
very bad with doubles.

Reviewers: campbellbarton, moguri, lordloki

Reviewed By: lordloki

Subscribers: brecht, lordloki

Differential Revision: https://developer.blender.org/D1610

16 files changed:
intern/moto/include/MT_CmMatrix4x4.h
intern/moto/include/MT_Scalar.h
intern/moto/intern/MT_CmMatrix4x4.cpp
source/gameengine/Ketsji/KX_FontObject.cpp
source/gameengine/Ketsji/KX_GameObject.cpp
source/gameengine/Ketsji/KX_GameObject.h
source/gameengine/Ketsji/KX_Scene.cpp
source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp
source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h
source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h
source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h
source/gameengine/Rasterizer/RAS_IRasterizer.h
source/gameengine/Rasterizer/RAS_MaterialBucket.h
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLLight.cpp
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.cpp
source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLRasterizer.h

index 2b710c668889cdd80fb7c1192a175b53e2b8d59a..53fdd41fb288d29eaf3bafeb83398f93aa39d44d 100644 (file)
@@ -55,7 +55,7 @@ class MT_CmMatrix4x4
 public :
 
        MT_CmMatrix4x4(
-               const float value[4][4]
+               const MT_Scalar value[4][4]
        );
 
        MT_CmMatrix4x4(
@@ -63,7 +63,7 @@ public :
 
 
        MT_CmMatrix4x4(
-               const double value[16]
+               const MT_Scalar value[16]
        );
 
        MT_CmMatrix4x4(
@@ -85,19 +85,19 @@ public :
                const MT_CmMatrix4x4 & other
        );
 
-               double*
+               MT_Scalar*
        getPointer(
        );
 
        const
-               double*
+               MT_Scalar*
        getPointer(
        ) const;
 
                void
        setElem(
                int pos,
-               double newvalue
+               MT_Scalar newvalue
        );
 
                MT_Vector3
@@ -121,7 +121,7 @@ public :
                const MT_Vector3 & v
        );
 
-               double&
+               MT_Scalar&
        operator (
        ) (int row,int col)     { return m_V[col][row]; }
 
@@ -139,8 +139,8 @@ public :
 protected:
        union
        {
-               double m_V[4][4];
-               double m_Vflat[16];
+               MT_Scalar m_V[4][4];
+               MT_Scalar m_Vflat[16];
        };
 };
 
index 5e516292d773d11b40eb4433d74891debd62ae50..6082e2d53687ec5d4d8ed57fbbcfc4c9315969d0 100644 (file)
@@ -52,7 +52,7 @@
 
 #include "MT_random.h"
 
-typedef double MT_Scalar; //this should be float !
+typedef float MT_Scalar;
 
 
 const MT_Scalar  MT_DEGS_PER_RAD(57.29577951308232286465);
index 7a04864e48dcb2d15c5d31dd41c0f29201ad412c..7eae14cb4d1233cffe57012109bb5d8501189b58 100644 (file)
@@ -42,7 +42,7 @@ MT_CmMatrix4x4::MT_CmMatrix4x4()
 
 
 
-MT_CmMatrix4x4::MT_CmMatrix4x4(const float value[4][4])
+MT_CmMatrix4x4::MT_CmMatrix4x4(const MT_Scalar value[4][4])
 {
        for (int i=0;i<4;i++)
        {
@@ -53,7 +53,7 @@ MT_CmMatrix4x4::MT_CmMatrix4x4(const float value[4][4])
 
 
 
-MT_CmMatrix4x4::MT_CmMatrix4x4(const double value[16])
+MT_CmMatrix4x4::MT_CmMatrix4x4(const MT_Scalar value[16])
 {
        for (int i=0;i<16;i++)
                m_Vflat[i] = value[i];
@@ -148,21 +148,21 @@ void MT_CmMatrix4x4::SetMatrix(const MT_CmMatrix4x4& other)
 
 
 
-double*        MT_CmMatrix4x4::getPointer()
+MT_Scalar*     MT_CmMatrix4x4::getPointer()
 {
        return &m_V[0][0];
 }
 
 
 
-const double* MT_CmMatrix4x4::getPointer() const
+const MT_Scalar* MT_CmMatrix4x4::getPointer() const
 {
        return &m_V[0][0];
 }      
 
 
 
-void MT_CmMatrix4x4::setElem(int pos,double newvalue)
+void MT_CmMatrix4x4::setElem(int pos,MT_Scalar newvalue)
 {
        m_Vflat[pos] = newvalue;
 }      
index 420f1f7eb98332c364bd2174b9673e7756d17ca9..13a5ec6c411912c637f6b6f98a7f4841e74d66f7 100644 (file)
@@ -194,7 +194,10 @@ void KX_FontObject::DrawFontText()
 
        /* Get a working copy of the OpenGLMatrix to use */
        double mat[16];
-       memcpy(mat, this->GetOpenGLMatrix(), sizeof(double)*16);
+       float *origmat = GetOpenGLMatrix();
+       for (unsigned short i = 0; i < 16; ++i) {
+               mat[i] = (double)origmat[i];
+       }
 
        /* Account for offset */
        MT_Vector3 offset = this->NodeGetWorldOrientation() * m_offset * this->NodeGetWorldScaling();
index 412a112df6b9cac9aa52a974e5b220213b102d6c..38d8a303fe8ad9381ec0a2a0d0b7f8fae8e83a3c 100644 (file)
@@ -700,10 +700,10 @@ void KX_GameObject::ApplyRotation(const MT_Vector3& drot,bool local)
 /**
  * GetOpenGL Matrix, returns an OpenGL 'compatible' matrix
  */
-double*        KX_GameObject::GetOpenGLMatrix()
+float *KX_GameObject::GetOpenGLMatrix()
 {
        // todo: optimize and only update if necessary
-       double* fl = m_OpenGL_4x4Matrix.getPointer();
+       float *fl = m_OpenGL_4x4Matrix.getPointer();
        if (GetSGNode()) {
                MT_Transform trans;
        
@@ -742,7 +742,7 @@ void KX_GameObject::AddMeshUser()
                m_meshes[i]->AddMeshUser(this, &m_meshSlots, GetDeformer());
        }
        // set the part of the mesh slot that never change
-       double* fl = GetOpenGLMatrixPtr()->getPointer();
+       float *fl = GetOpenGLMatrixPtr()->getPointer();
 
        SG_QList::iterator<RAS_MeshSlot> mit(m_meshSlots);
 //     RAS_MeshSlot* ms;
@@ -2768,7 +2768,7 @@ PyObject *KX_GameObject::pyattr_get_localTransform(void *self_v, const KX_PYATTR
 {
        KX_GameObject* self = static_cast<KX_GameObject*>(self_v);
 
-       double mat[16];
+       float mat[16];
 
        MT_Transform trans;
        
index c2c455dab6a0d321761bb24e0fbd3e73b4b1714a..a25f999026c586ac175c9d66d00b59faebba918e 100644 (file)
@@ -175,7 +175,7 @@ public:
         * side effect of storing the result internally. The
         * memory for the matrix remains the property of this class.
         */ 
-               double *
+               float *
        GetOpenGLMatrix(
        );
 
index 16d1fdd6ea2a04233602829107dc2982e6fd9048..350bad0a3366cbaa19f05e68c6b095c5a597a7f7 100644 (file)
@@ -1557,9 +1557,9 @@ void KX_Scene::CalculateVisibleMeshes(RAS_IRasterizer* rasty,KX_Camera* cam, int
                planes[5].setValue(cplanes[3].getValue());      // bottom
                CullingInfo info(layer);
 
-               double mvmat[16] = {0};
+               float mvmat[16] = {0};
                cam->GetModelviewMatrix().getValue(mvmat);
-               double pmat[16] = {0};
+               float pmat[16] = {0};
                cam->GetProjectionMatrix().getValue(pmat);
 
                dbvt_culling = m_physicsEnvironment->CullingTest(PhysicsCullingCallback,&info,planes,5,m_dbvt_occlusion_res,
index d7b043661a0de8749daf45f7b886dbb40b35e7f2..9f0fc6d9d4db623b4917caed1489a19cd2582a04 100644 (file)
@@ -1472,7 +1472,7 @@ struct OcclusionBuffer
                m[15] = btScalar(m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15]);
        }
 
-       void setup(int size, const int *view, double modelview[16], double projection[16])
+       void setup(int size, const int *view, float modelview[16], float projection[16])
        {
                m_initialized = false;
                m_occlusion = false;
@@ -1519,7 +1519,7 @@ struct OcclusionBuffer
                m_occlusion = false;
        }
 
-       void SetModelMatrix(double *fl)
+       void SetModelMatrix(float *fl)
        {
                CMmat4mul(m_mtc,m_wtc,fl);
                if (!m_initialized) {
@@ -1969,7 +1969,7 @@ struct    DbvtCullingCallback : btDbvt::ICollide
                        KX_GameObject* gameobj = KX_GameObject::GetClientObject(info);
                        if (gameobj && gameobj->GetOccluder())
                        {
-                               double* fl = gameobj->GetOpenGLMatrixPtr()->getPointer();
+                               float *fl = gameobj->GetOpenGLMatrixPtr()->getPointer();
                                // this will create the occlusion buffer if not already done
                                // and compute the transformation from model local space to clip space
                                m_ocb->SetModelMatrix(fl);
@@ -2010,7 +2010,7 @@ struct    DbvtCullingCallback : btDbvt::ICollide
 };
 
 static OcclusionBuffer gOcb;
-bool CcdPhysicsEnvironment::CullingTest(PHY_CullingCallback callback, void* userData, MT_Vector4 *planes, int nplanes, int occlusionRes, const int *viewport, double modelview[16], double projection[16])
+bool CcdPhysicsEnvironment::CullingTest(PHY_CullingCallback callback, void* userData, MT_Vector4 *planes, int nplanes, int occlusionRes, const int *viewport, float modelview[16], float projection[16])
 {
        if (!m_cullingTree)
                return false;
index 86f663959f1c815912286a065c7b3fa8d8e2bd36..a64d2c8f15ff0e00cd23ca0bbfa899dba149dc7a 100644 (file)
@@ -211,7 +211,7 @@ protected:
                btTypedConstraint*      GetConstraintById(int constraintId);
 
                virtual PHY_IPhysicsController* RayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ);
-               virtual bool CullingTest(PHY_CullingCallback callback, void* userData, MT_Vector4* planes, int nplanes, int occlusionRes, const int *viewport, double modelview[16], double projection[16]);
+               virtual bool CullingTest(PHY_CullingCallback callback, void* userData, MT_Vector4* planes, int nplanes, int occlusionRes, const int *viewport, float modelview[16], float projection[16]);
 
 
                //Methods for gamelogic collision/physics callbacks
index 929345d71613d317e76a01fe798f00d93a0d8de8..3e9379dd60d9fe70e8f5688711b1096c4ab905fa 100644 (file)
@@ -83,7 +83,7 @@ public:
        }
 
        virtual PHY_IPhysicsController* RayTest(PHY_IRayCastFilterCallback &filterCallback, float fromX,float fromY,float fromZ, float toX,float toY,float toZ);
-       virtual bool CullingTest(PHY_CullingCallback callback, void* userData, class MT_Vector4* planes, int nplanes, int occlusionRes, const int *viewport, double modelview[16], double projection[16]) { return false; }
+       virtual bool CullingTest(PHY_CullingCallback callback, void* userData, class MT_Vector4* planes, int nplanes, int occlusionRes, const int *viewport, float modelview[16], float projection[16]) { return false; }
 
 
        //gamelogic callbacks
index 4b8d36285bef2b021ac0a7abb4d137a623991275..2997048805f960b20589b5a8d83ac65e98ad67cf 100644 (file)
@@ -183,7 +183,7 @@ class PHY_IPhysicsEnvironment
                //culling based on physical broad phase
                // the plane number must be set as follow: near, far, left, right, top, botton
                // the near plane must be the first one and must always be present, it is used to get the direction of the view
-               virtual bool CullingTest(PHY_CullingCallback callback, void *userData, MT_Vector4* planeNormals, int planeNumber, int occlusionRes, const int *viewport, double modelview[16], double projection[16]) = 0;
+               virtual bool CullingTest(PHY_CullingCallback callback, void *userData, MT_Vector4* planeNormals, int planeNumber, int occlusionRes, const int *viewport, float modelview[16], float projection[16]) = 0;
 
                //Methods for gamelogic collision/physics callbacks
                //todo:
index e960131c1fb86041878085b3311154bba4612ed9..aa32f6254754c436be933fea5ae68c7809846efb 100644 (file)
@@ -422,7 +422,7 @@ public:
        /**
         * Render Tools
         */
-       virtual void applyTransform(double *oglmatrix, int drawingmode) = 0;
+       virtual void applyTransform(float *oglmatrix, int drawingmode) = 0;
 
        /**
         * Renders 2D boxes.
index 1eee99f896b0f3f2ad45b3255c27cde84dc81760..75cc382c78c94e9e2aab242ae32e35d75655e3d5 100644 (file)
@@ -128,7 +128,7 @@ public:
        void*                                   m_clientObj;
        RAS_Deformer*                   m_pDeformer;
        DerivedMesh*                    m_pDerivedMesh;
-       double*                                 m_OpenGLMatrix;
+       float*                                  m_OpenGLMatrix;
        // visibility
        bool                                    m_bVisible;
        bool                                    m_bCulled;
index 4ac1c9c4ebb4a5e20bdd6d7f64ca5229dda616e7..c6d9981cb3cf10f06f7877ad6bb1796332447f58 100644 (file)
@@ -262,7 +262,7 @@ void RAS_OpenGLLight::Update()
                // lights don't get their openGL matrix updated, do it now
                if (kxlight->GetSGNode()->IsDirty())
                        kxlight->GetOpenGLMatrix();
-               double *dobmat = kxlight->GetOpenGLMatrixPtr()->getPointer();
+               float *dobmat = kxlight->GetOpenGLMatrixPtr()->getPointer();
 
                for (int i=0; i<4; i++)
                        for (int j=0; j<4; j++, dobmat++)
index bbdf17b5bc4d89de6a2bfb7d9bb7ceb0f3d34bbb..54cffcc3c18769de1058f8ed8d757fa91a10c6ba 100644 (file)
@@ -371,8 +371,8 @@ void RAS_OpenGLRasterizer::FlushDebugShapes(SCA_IScene *scene)
                glColor4f(debugShapes[i].m_color[0], debugShapes[i].m_color[1], debugShapes[i].m_color[2], 1.0f);
                const MT_Scalar *fromPtr = &debugShapes[i].m_pos.x();
                const MT_Scalar *toPtr= &debugShapes[i].m_param.x();
-               glVertex3dv(fromPtr);
-               glVertex3dv(toPtr);
+               glVertex3fv(fromPtr);
+               glVertex3fv(toPtr);
        }
        glEnd();
 
@@ -408,7 +408,7 @@ void RAS_OpenGLRasterizer::FlushDebugShapes(SCA_IScene *scene)
                        pos = pos*tr;
                        pos += debugShapes[i].m_pos;
                        const MT_Scalar* posPtr = &pos.x();
-                       glVertex3dv(posPtr);
+                       glVertex3fv(posPtr);
                }
                glEnd();
        }
@@ -823,8 +823,8 @@ void RAS_OpenGLRasterizer::DrawDerivedMesh(class RAS_MeshSlot &ms)
 void RAS_OpenGLRasterizer::SetProjectionMatrix(MT_CmMatrix4x4 &mat)
 {
        glMatrixMode(GL_PROJECTION);
-       double* matrix = &mat(0, 0);
-       glLoadMatrixd(matrix);
+       float* matrix = &mat(0, 0);
+       glLoadMatrixf(matrix);
 
        m_camortho = (mat(3, 3) != 0.0);
 }
@@ -832,11 +832,11 @@ void RAS_OpenGLRasterizer::SetProjectionMatrix(MT_CmMatrix4x4 &mat)
 void RAS_OpenGLRasterizer::SetProjectionMatrix(const MT_Matrix4x4 & mat)
 {
        glMatrixMode(GL_PROJECTION);
-       double matrix[16];
+       float matrix[16];
        /* Get into argument. Looks a bit dodgy, but it's ok. */
        mat.getValue(matrix);
        /* Internally, MT_Matrix4x4 uses doubles (MT_Scalar). */
-       glLoadMatrixd(matrix);
+       glLoadMatrixf(matrix);
 
        m_camortho= (mat[3][3] != 0.0);
 }
@@ -852,7 +852,7 @@ MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
        bool 
 ) {
        MT_Matrix4x4 result;
-       double mat[16];
+       float mat[16];
 
        // correction for stereo
        if (Stereo())
@@ -891,7 +891,7 @@ MT_Matrix4x4 RAS_OpenGLRasterizer::GetFrustumMatrix(
        glLoadIdentity();
        glFrustum(left, right, bottom, top, frustnear, frustfar);
                
-       glGetDoublev(GL_PROJECTION_MATRIX, mat);
+       glGetFloatv(GL_PROJECTION_MATRIX, mat);
        result.setValue(mat);
 
        return result;
@@ -906,14 +906,14 @@ MT_Matrix4x4 RAS_OpenGLRasterizer::GetOrthoMatrix(
        float frustfar
 ) {
        MT_Matrix4x4 result;
-       double mat[16];
+       float mat[16];
 
        // stereo is meaning less for orthographic, disable it
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(left, right, bottom, top, frustnear, frustfar);
                
-       glGetDoublev(GL_PROJECTION_MATRIX, mat);
+       glGetFloatv(GL_PROJECTION_MATRIX, mat);
        result.setValue(mat);
 
        return result;
@@ -974,7 +974,7 @@ void RAS_OpenGLRasterizer::SetViewMatrix(const MT_Matrix4x4 &mat,
        m_viewmatrix.getValue(glviewmat);
 
        glMatrixMode(GL_MODELVIEW);
-       glLoadMatrixd(glviewmat);
+       glLoadMatrixf(glviewmat);
        m_campos = pos;
 }
 
@@ -1287,7 +1287,7 @@ void RAS_OpenGLRasterizer::RemoveLight(RAS_ILightObject* lightobject)
                m_lights.erase(lit);
 }
 
-bool RAS_OpenGLRasterizer::RayHit(struct KX_ClientObjectInfo *client, KX_RayCast *result, double *oglmatrix)
+bool RAS_OpenGLRasterizer::RayHit(struct KX_ClientObjectInfo *client, KX_RayCast *result, float *oglmatrix)
 {
        if (result->m_hitMesh) {
 
@@ -1301,14 +1301,14 @@ bool RAS_OpenGLRasterizer::RayHit(struct KX_ClientObjectInfo *client, KX_RayCast
                left = (dir.cross(resultnormal)).safe_normalized();
                // for the up vector, we take the 'resultnormal' returned by the physics
 
-               double maat[16] = {left[0],         left[1],         left[2],         0,
+               float maat[16] = {left[0],         left[1],         left[2],         0,
                                       dir[0],          dir[1],          dir[2],          0,
                                           resultnormal[0], resultnormal[1], resultnormal[2], 0,
                                               0,               0,               0,               1};
 
-               glTranslated(oglmatrix[12],oglmatrix[13],oglmatrix[14]);
+               glTranslatef(oglmatrix[12],oglmatrix[13],oglmatrix[14]);
                //glMultMatrixd(oglmatrix);
-               glMultMatrixd(maat);
+               glMultMatrixf(maat);
                return true;
        }
        else {
@@ -1316,7 +1316,7 @@ bool RAS_OpenGLRasterizer::RayHit(struct KX_ClientObjectInfo *client, KX_RayCast
        }
 }
 
-void RAS_OpenGLRasterizer::applyTransform(double* oglmatrix,int objectdrawmode )
+void RAS_OpenGLRasterizer::applyTransform(float* oglmatrix,int objectdrawmode )
 {
        /* FIXME:
        blender: intern/moto/include/MT_Vector3.inl:42: MT_Vector3 operator/(const
@@ -1369,13 +1369,13 @@ void RAS_OpenGLRasterizer::applyTransform(double* oglmatrix,int objectdrawmode )
                dir  *= size[1];
                up   *= size[2];
 
-               double maat[16] = {left[0], left[1], left[2], 0,
+               float maat[16] = {left[0], left[1], left[2], 0,
                                   dir[0],  dir[1],  dir[2],  0,
                                   up[0],   up[1],   up[2],   0,
                                   0,       0,       0,       1};
 
                glTranslatef(objpos[0],objpos[1],objpos[2]);
-               glMultMatrixd(maat);
+               glMultMatrixf(maat);
 
        }
        else {
@@ -1399,11 +1399,11 @@ void RAS_OpenGLRasterizer::applyTransform(double* oglmatrix,int objectdrawmode )
                        if (!physics_controller && parent)
                                physics_controller = parent->GetPhysicsController();
 
-                       KX_RayCast::Callback<RAS_OpenGLRasterizer, double> callback(this, physics_controller, oglmatrix);
+                       KX_RayCast::Callback<RAS_OpenGLRasterizer, float> callback(this, physics_controller, oglmatrix);
                        if (!KX_RayCast::RayTest(physics_environment, frompoint, topoint, callback))
                        {
                                // couldn't find something to cast the shadow on...
-                               glMultMatrixd(oglmatrix);
+                               glMultMatrixf(oglmatrix);
                        }
                        else
                        { // we found the "ground", but the cast matrix doesn't take
@@ -1415,7 +1415,7 @@ void RAS_OpenGLRasterizer::applyTransform(double* oglmatrix,int objectdrawmode )
                {
 
                        // 'normal' object
-                       glMultMatrixd(oglmatrix);
+                       glMultMatrixf(oglmatrix);
                }
        }
 }
index 1b012a61355a135269b6e2de302c8ab8b5c47f9e..d6b2c3c8a30672bae41ce4fb200c1c5d6b0a44b4 100644 (file)
@@ -301,13 +301,13 @@ public:
        void RenderText2D(RAS_TEXT_RENDER_MODE mode, const char *text,
                          int xco, int yco, int width, int height);
 
-       void applyTransform(double *oglmatrix, int objectdrawmode);
+       void applyTransform(float *oglmatrix, int objectdrawmode);
 
        void PushMatrix();
        void PopMatrix();
 
        /// \see KX_RayCast
-       bool RayHit(struct KX_ClientObjectInfo *client, class KX_RayCast *result, double *oglmatrix);
+       bool RayHit(struct KX_ClientObjectInfo *client, class KX_RayCast *result, float *oglmatrix);
        /// \see KX_RayCast
        bool NeedRayCast(struct KX_ClientObjectInfo *, void *UNUSED(data)) { return true; }