featureMgr.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef _FEATUREMGR_H_
  23. #define _FEATUREMGR_H_
  24. #ifndef _TSINGLETON_H_
  25. #include "core/util/tSingleton.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #ifndef _UTIL_DELEGATE_H_
  31. #include "core/util/delegate.h"
  32. #endif
  33. class FeatureType;
  34. class ShaderFeature;
  35. typedef Delegate<ShaderFeature* (void*)> CreateShaderFeatureDelegate;
  36. /// <summary>
  37. /// Used by the feature manager.
  38. /// </summary>
  39. /// <param name="type">The shader feature type.</param>
  40. /// <param name="feature">The shader feature class.</param>
  41. /// <param name="createFunc">The static create function for this feature.</param>
  42. struct FeatureInfo
  43. {
  44. const FeatureType *type;
  45. ShaderFeature *feature;
  46. CreateShaderFeatureDelegate createFunc;
  47. };
  48. ///
  49. class FeatureMgr
  50. {
  51. protected:
  52. bool mNeedsSort;
  53. typedef Vector<FeatureInfo> FeatureInfoVector;
  54. FeatureInfoVector mFeatures;
  55. static S32 QSORT_CALLBACK _featureInfoCompare( const FeatureInfo *a, const FeatureInfo *b );
  56. public:
  57. FeatureMgr();
  58. ~FeatureMgr();
  59. /// Returns the count of registered features.
  60. U32 getFeatureCount() const { return mFeatures.size(); }
  61. /// Returns the feature info at the index.
  62. const FeatureInfo& getAt( U32 index );
  63. ///
  64. ShaderFeature* getByType( const FeatureType &type );
  65. /// <summary>
  66. /// Creates a shader feature of this type with the arguments provided.
  67. /// </summary>
  68. /// <param name="type">The shader feature type.</param>
  69. /// <param name="argStruct">The arguments for setting up this isntance of the shaderFeature.</param>
  70. /// <returns>An instance of the shader feature using its static createFunction taking in the
  71. /// argument struct.
  72. /// </returns>
  73. ShaderFeature* createFeature(const FeatureType& type, void* argStruct);
  74. /// <summary>
  75. /// Allows other systems to add features. index is
  76. /// the enum in GFXMaterialFeatureData.
  77. /// </summary>
  78. /// <param name="type">The shader feature type.</param>
  79. /// <param name="feature">The shader feature (can be null if featureDelegate defined)</param>
  80. /// <param name="featureDelegate">The feature delegate create function.</param>
  81. void registerFeature(const FeatureType& type,
  82. ShaderFeature* feature = nullptr,
  83. CreateShaderFeatureDelegate featureDelegate = nullptr);
  84. // Unregister a feature.
  85. void unregisterFeature( const FeatureType &type );
  86. /// Removes all features.
  87. void unregisterAll();
  88. // For ManagedSingleton.
  89. static const char* getSingletonName() { return "FeatureMgr"; }
  90. };
  91. // Helper for accessing the feature manager singleton.
  92. #define FEATUREMGR ManagedSingleton<FeatureMgr>::instance()
  93. #endif // FEATUREMGR