featureMgr.cpp 3.8 KB

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