# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

load_balancing_trav.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 
00028 #include "3d/load_balancing_trav.h"
00029 #include "3d/hrc_trav.h"
00030 #include "3d/clip_trav.h"
00031 #include "nel/misc/common.h"
00032 #include "nel/misc/hierarchical_timer.h"
00033 
00034 
00035 using namespace std;
00036 using namespace NLMISC;
00037 
00038 
00039 // ***************************************************************************
00040 #define NL3D_DEFAULT_LOADBALANCING_VALUE_SMOOTHER       50
00041 #define NL3D_LOADBALANCING_SMOOTHER_MAX_RATIO           1.1f
00042 
00043 namespace NL3D 
00044 {
00045 
00046 // ***************************************************************************
00047 // ***************************************************************************
00048 // ***************************************************************************
00049 // ***************************************************************************
00050 
00051 
00052 // ***************************************************************************
00053 CLoadBalancingGroup::CLoadBalancingGroup()
00054 {
00055         _PrecPolygonBalancingMode= CLoadBalancingGroup::PolygonBalancingOff;
00056         _NbFaceWanted= 20000;
00057         _ValueSmoother.init(NL3D_DEFAULT_LOADBALANCING_VALUE_SMOOTHER);
00058         _DefaultGroup= false;
00059 
00060         _NbFacePass0= 0;
00061         _FaceRatio= 1;
00062 }
00063 
00064 
00065 // ***************************************************************************
00066 void                    CLoadBalancingGroup::computeRatioAndSmooth(TPolygonBalancingMode polMode)
00067 {
00068         // If Default group, disable load balancing
00069         if(_DefaultGroup)
00070                 polMode= PolygonBalancingOff;
00071 
00072         // Compute ratio
00073         switch(polMode)
00074         {
00075         case PolygonBalancingOff:
00076                 _FaceRatio= 1;
00077                 break;
00078         case PolygonBalancingOn :
00079                 if(_NbFacePass0!=0)
00080                         _FaceRatio= (float)_NbFaceWanted / _NbFacePass0;
00081                 else
00082                         _FaceRatio= 1;
00083                 break;
00084         case PolygonBalancingClamp:
00085                 if(_NbFacePass0!=0)
00086                         _FaceRatio= (float)_NbFaceWanted / _NbFacePass0;
00087                 else
00088                         _FaceRatio= 1;
00089                 clamp(_FaceRatio, 0, 1);
00090                 break;
00091                 default: break;
00092         };
00093 
00094         // smooth the value.
00095         // if change of PolygonBalancingMode, reset the _ValueSmoother.
00096         if(polMode!=_PrecPolygonBalancingMode)
00097         {
00098                 _ValueSmoother.init(NL3D_DEFAULT_LOADBALANCING_VALUE_SMOOTHER);
00099                 _PrecPolygonBalancingMode= polMode;
00100         }
00101         // if not PolygonBalancingOff, smooth the ratio.
00102         if(polMode!=PolygonBalancingOff)
00103         {
00104                 _ValueSmoother.addValue(_FaceRatio);
00105                 float   fSmooth= _ValueSmoother.getSmoothValue();
00106 
00107                 // If after smoothing, the number of faces is still too big, reduce smooth effect! (frustrum clip effect)
00108                 if(fSmooth*_NbFacePass0 > _NbFaceWanted*NL3D_LOADBALANCING_SMOOTHER_MAX_RATIO)
00109                 {
00110                         // reset the smoother
00111                         _ValueSmoother.reset();
00112                         // reduce smooth effect
00113                         fSmooth= _FaceRatio*NL3D_LOADBALANCING_SMOOTHER_MAX_RATIO;
00114                         _ValueSmoother.addValue(fSmooth);
00115                 }
00116 
00117                 // take the smoothed value.
00118                 _FaceRatio= fSmooth;
00119         }
00120 
00121 
00122 }
00123 
00124 
00125 
00126 // ***************************************************************************
00127 // ***************************************************************************
00128 // ***************************************************************************
00129 // ***************************************************************************
00130 
00131 
00132 // ***************************************************************************
00133 IObs                            *CLoadBalancingTrav::createDefaultObs() const
00134 {
00135         return new CDefaultLoadBalancingObs;
00136 }
00137 
00138 
00139 // ***************************************************************************
00140 CLoadBalancingTrav::CLoadBalancingTrav()
00141 {
00142         PolygonBalancingMode= CLoadBalancingGroup::PolygonBalancingOff;
00143 
00144         // Add the default group and make it default
00145         _GroupMap["Default"].Name= "Default";
00146         _GroupMap["Default"]._DefaultGroup= true;
00147 
00148         // set the DefaultGroup ptr.
00149         _DefaultGroup= &_GroupMap["Default"];
00150 
00151         // prepare some space
00152         _VisibleList.reserve(1024);
00153 }
00154 
00155 
00156 // ***************************************************************************
00157 void                            CLoadBalancingTrav::clearVisibleList()
00158 {
00159         _VisibleList.clear();
00160 }
00161 
00162 // ***************************************************************************
00163 void                            CLoadBalancingTrav::addVisibleObs(IBaseLoadBalancingObs *obs)
00164 {
00165         _VisibleList.push_back(obs);
00166 }
00167 
00168 
00169 // ***************************************************************************
00170 void                            CLoadBalancingTrav::traverse()
00171 {
00172         H_AUTO( NL3D_TravLoadBalancing );
00173 
00174         ITravCameraScene::update();
00175 
00176         // Reset each group.
00177         //================
00178         ItGroupMap      it= _GroupMap.begin();
00179         for(;it!=_GroupMap.end();it++)
00180         {
00181                 // reset _NbFacePass0.
00182                 it->second._NbFacePass0= 0;
00183         }
00184 
00185 
00186         // Traverse the graph 2 times.
00187 
00188         // 1st pass, count NBFaces drawed.
00189         //================
00190         _LoadPass= 0;
00191         // count _NbFacePass0.
00192         traverseVisibilityList();
00193 
00194 
00195         // Reset _SumNbFacePass0
00196         _SumNbFacePass0= 0;
00197         // For each group
00198         it= _GroupMap.begin();
00199         for(;it!=_GroupMap.end();it++)
00200         {
00201                 // compute ratio and smooth
00202                 it->second.computeRatioAndSmooth(PolygonBalancingMode);
00203                 // update _SumNbFacePass0
00204                 _SumNbFacePass0+= it->second.getNbFaceAsked();
00205         }
00206 
00207 
00208         // 2nd pass, compute Faces that will be drawed.
00209         //================
00210         _LoadPass= 1;
00211         traverseVisibilityList();
00212 
00213 }
00214 
00215 
00216 // ***************************************************************************
00217 void                            CLoadBalancingTrav::traverseVisibilityList()
00218 {
00219         // Traverse all nodes of the visibility list.
00220         uint    nObs= _VisibleList.size();
00221         for(uint i=0; i<nObs; i++)
00222         {
00223                 IBaseLoadBalancingObs   *loadBalObs= _VisibleList[i];
00224                 loadBalObs->traverse(NULL);
00225         }
00226 }
00227 
00228 
00229 // ***************************************************************************
00230 float                           CLoadBalancingTrav::getNbFaceAsked () const
00231 {
00232         return _SumNbFacePass0;
00233 }
00234 
00235 
00236 // ***************************************************************************
00237 CLoadBalancingGroup     *CLoadBalancingTrav::getOrCreateGroup(const std::string &group)
00238 {
00239         // find
00240         ItGroupMap      it;
00241         it= _GroupMap.find(group);
00242         // if not exist, create.
00243         if(it==_GroupMap.end())
00244         {
00245                 // create and set name.
00246                 it= _GroupMap.insert(make_pair(group, CLoadBalancingGroup())).first;
00247                 it->second.Name= group;
00248         }
00249 
00250         return &(it->second);
00251 }
00252 
00253 // ***************************************************************************
00254 void                            CLoadBalancingTrav::setGroupNbFaceWanted(const std::string &group, uint nFaces)
00255 {
00256         // get/create if needed, and assign.
00257         getOrCreateGroup(group)->setNbFaceWanted(nFaces);
00258 }
00259 
00260 // ***************************************************************************
00261 uint                            CLoadBalancingTrav::getGroupNbFaceWanted(const std::string &group)
00262 {
00263         // get/create if needed, and get.
00264         return getOrCreateGroup(group)->getNbFaceWanted();
00265 }
00266 
00267 // ***************************************************************************
00268 float                           CLoadBalancingTrav::getGroupNbFaceAsked (const std::string &group) const
00269 {
00270         TGroupMap::const_iterator       it;
00271         it= _GroupMap.find(group);
00272         if(it==_GroupMap.end())
00273                 return 0;
00274         else
00275                 return it->second.getNbFaceAsked();
00276 }
00277 
00278 
00279 // ***************************************************************************
00280 // ***************************************************************************
00281 // ***************************************************************************
00282 // ***************************************************************************
00283 
00284 
00285 // ***************************************************************************
00286 void    IBaseLoadBalancingObs::init()
00287 {
00288         IObs::init();
00289         nlassert( dynamic_cast<IBaseHrcObs*> (getObs(HrcTravId)) );
00290         HrcObs= static_cast<IBaseHrcObs*> (getObs(HrcTravId));
00291         nlassert( dynamic_cast<IBaseClipObs*> (getObs(ClipTravId)) );
00292         ClipObs= static_cast<IBaseClipObs*> (getObs(ClipTravId));
00293 
00294         // assign me to the default group
00295         LoadBalancingGroup= ((CLoadBalancingTrav*)Trav)->getDefaultGroup();
00296 }
00297 
00298 
00299 
00300 
00301 } // NL3D