featureMgr.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. ShaderFeature* FeatureMgr::createFeature(const FeatureType& type, void* argStruct)
  81. {
  82. FeatureInfoVector::iterator iter = mFeatures.begin();
  83. for (; iter != mFeatures.end(); iter++)
  84. {
  85. if (*iter->type == type)
  86. {
  87. if (iter->createFunc != NULL)
  88. {
  89. return iter->createFunc(argStruct);
  90. }
  91. }
  92. }
  93. return nullptr;
  94. }
  95. void FeatureMgr::registerFeature( const FeatureType &type,
  96. ShaderFeature *feature,
  97. CreateShaderFeatureDelegate featureDelegate)
  98. {
  99. if (feature == nullptr && featureDelegate == nullptr)
  100. {
  101. AssertFatal(false, "FeatureMgr::registerFeature - no feature or featureDelegate defined, cannot create this feature.");
  102. }
  103. // Remove any existing feature.
  104. unregisterFeature( type );
  105. // Now add the new feature.
  106. mFeatures.increment();
  107. mFeatures.last().type = &type;
  108. mFeatures.last().feature = feature;
  109. mFeatures.last().createFunc = featureDelegate;
  110. // Make sure we resort the features.
  111. mNeedsSort = true;
  112. }
  113. S32 QSORT_CALLBACK FeatureMgr::_featureInfoCompare( const FeatureInfo* a, const FeatureInfo* b )
  114. {
  115. const FeatureType *typeA = a->type;
  116. const FeatureType *typeB = b->type;
  117. if ( typeA->getGroup() < typeB->getGroup() )
  118. return -1;
  119. else if ( typeA->getGroup() > typeB->getGroup() )
  120. return 1;
  121. else if ( typeA->getOrder() < typeB->getOrder() )
  122. return -1;
  123. else if ( typeA->getOrder() > typeB->getOrder() )
  124. return 1;
  125. else
  126. return 0;
  127. }
  128. void FeatureMgr::unregisterFeature( const FeatureType &type )
  129. {
  130. FeatureInfoVector::iterator iter = mFeatures.begin();
  131. for ( ; iter != mFeatures.end(); iter++ )
  132. {
  133. if ( *iter->type != type )
  134. continue;
  135. delete iter->feature;
  136. mFeatures.erase( iter );
  137. return;
  138. }
  139. }