featureMgr.cpp

Engine/source/shaderGen/featureMgr.cpp

More...

Public Variables

Public Functions

Detailed Description

Public Variables

 MODULE_END 
 MODULE_SHUTDOWN 

Public Functions

MODULE_SHUTDOWN_AFTER(Sim )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "shaderGen/featureMgr.h"
 26
 27#include "shaderGen/featureType.h"
 28#include "shaderGen/shaderFeature.h"
 29#include "core/util/safeDelete.h"
 30#include "core/module.h"
 31
 32
 33MODULE_BEGIN( ShaderGenFeatureMgr )
 34
 35   MODULE_INIT_BEFORE( ShaderGen )
 36   MODULE_SHUTDOWN_AFTER( Sim ) // allow registered features to be removed before destroying singleton
 37
 38   MODULE_INIT
 39   {
 40      ManagedSingleton< FeatureMgr >::createSingleton();
 41   }
 42   
 43   MODULE_SHUTDOWN
 44   {
 45      ManagedSingleton< FeatureMgr >::deleteSingleton();
 46   }
 47
 48MODULE_END;
 49
 50
 51FeatureMgr::FeatureMgr()
 52   : mNeedsSort( false )
 53{
 54   VECTOR_SET_ASSOCIATION( mFeatures );
 55}
 56
 57FeatureMgr::~FeatureMgr()
 58{
 59   unregisterAll();
 60}
 61
 62void FeatureMgr::unregisterAll()
 63{
 64   FeatureInfoVector::iterator iter = mFeatures.begin();
 65   for ( ; iter != mFeatures.end(); iter++ )
 66   {
 67      if ( iter->feature )
 68         delete iter->feature;
 69   }
 70
 71   mFeatures.clear();
 72   mNeedsSort = false;
 73}
 74
 75const FeatureInfo& FeatureMgr::getAt( U32 index )
 76{
 77   if ( mNeedsSort )
 78   {
 79      mFeatures.sort( _featureInfoCompare );
 80      mNeedsSort = false;
 81   }
 82
 83   AssertFatal( index < mFeatures.size(), "FeatureMgr::getAt() - Index out of range!" );
 84
 85   return mFeatures[index];
 86}
 87
 88ShaderFeature* FeatureMgr::getByType( const FeatureType &type )
 89{
 90   FeatureInfoVector::iterator iter = mFeatures.begin();
 91   for ( ; iter != mFeatures.end(); iter++ )
 92   {
 93      if ( *iter->type == type )
 94         return iter->feature;
 95   }
 96
 97   return NULL;
 98}
 99
100void FeatureMgr::registerFeature(   const FeatureType &type, 
101                                    ShaderFeature *feature )
102{
103   // Remove any existing feature first.
104   unregisterFeature( type );
105
106   // Now add the new feature.
107   mFeatures.increment();
108   mFeatures.last().type = &type;
109   mFeatures.last().feature = feature;
110
111   // Make sure we resort the features.
112   mNeedsSort = true;
113}
114
115S32 QSORT_CALLBACK FeatureMgr::_featureInfoCompare( const FeatureInfo* a, const FeatureInfo* b )
116{
117   const FeatureType *typeA = a->type;
118   const FeatureType *typeB = b->type;
119
120   if ( typeA->getGroup() < typeB->getGroup() )
121      return -1;
122   else if ( typeA->getGroup() > typeB->getGroup() )
123      return 1;
124   else if ( typeA->getOrder() < typeB->getOrder() )
125      return -1;
126   else if ( typeA->getOrder() > typeB->getOrder() )
127      return 1;
128   else
129      return 0;
130}
131
132void FeatureMgr::unregisterFeature( const FeatureType &type )
133{
134   FeatureInfoVector::iterator iter = mFeatures.begin();
135   for ( ; iter != mFeatures.end(); iter++ )
136   {
137      if ( *iter->type != type )
138         continue;
139
140      delete iter->feature;
141      mFeatures.erase( iter );
142      return;
143   }
144}
145