materialManager.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 _MATERIAL_MGR_H_
  23. #define _MATERIAL_MGR_H_
  24. #ifndef _MATERIALDEFINITION_H_
  25. #include "materials/materialDefinition.h"
  26. #endif
  27. #ifndef _FEATURESET_H_
  28. #include "shaderGen/featureSet.h"
  29. #endif
  30. #ifndef _GFXDEVICE_H_
  31. #include "gfx/gfxDevice.h"
  32. #endif
  33. #ifndef _TSINGLETON_H_
  34. #include "core/util/tSingleton.h"
  35. #endif
  36. class SimSet;
  37. class MatInstance;
  38. class GuiTreeViewCtrl;
  39. class MaterialManager : public ManagedSingleton<MaterialManager>
  40. {
  41. public:
  42. MaterialManager();
  43. ~MaterialManager();
  44. // ManagedSingleton
  45. static const char* getSingletonName() { return "MaterialManager"; }
  46. Material * allocateAndRegister(const String &objectName, const String &mapToName = String());
  47. Material * getMaterialDefinitionByName(const String &matName);
  48. Material* getMaterialDefinitionByMapTo(const String& mapTo);
  49. SimSet * getMaterialSet();
  50. // map textures to materials
  51. void mapMaterial(const String & textureName, const String & materialName);
  52. String getMapEntry(const String & textureName) const;
  53. // Return instance of named material caller is responsible for memory
  54. BaseMatInstance * createMatInstance( const String &matName );
  55. // Create a BaseMatInstance with the default feature flags.
  56. BaseMatInstance * createMatInstance( const String &matName, const GFXVertexFormat *vertexFormat );
  57. BaseMatInstance * createMatInstance( const String &matName, const FeatureSet &features, const GFXVertexFormat *vertexFormat );
  58. /// The default feature set for materials.
  59. const FeatureSet& getDefaultFeatures() const { return mDefaultFeatures; }
  60. /// The feature exclusion list for disabling features.
  61. const FeatureSet& getExclusionFeatures() const { return mExclusionFeatures; }
  62. void recalcFeaturesFromPrefs();
  63. /// Get the default texture anisotropy.
  64. U32 getDefaultAnisotropy() const { return mDefaultAnisotropy; }
  65. /// Allocate and return an instance of special materials. Caller is responsible for the memory.
  66. BaseMatInstance * createWarningMatInstance();
  67. /// Gets the global warning material instance, callers should not free this copy
  68. BaseMatInstance * getWarningMatInstance();
  69. /// Set the deferred enabled state.
  70. void setDeferredEnabled( bool enabled ) { mUsingDeferred = enabled; }
  71. /// Get the deferred enabled state.
  72. bool getDeferredEnabled() const { return mUsingDeferred; }
  73. #ifndef TORQUE_SHIPPING
  74. // Allocate and return an instance of mesh debugging materials. Caller is responsible for the memory.
  75. BaseMatInstance * createMeshDebugMatInstance(const LinearColorF &meshColor);
  76. // Gets the global material instance for a given color, callers should not free this copy
  77. BaseMatInstance * getMeshDebugMatInstance(const LinearColorF &meshColor);
  78. #endif
  79. void dumpMaterialInstances( BaseMaterialDefinition *target = NULL ) const;
  80. void getMaterialInstances(BaseMaterialDefinition* target, GuiTreeViewCtrl* tree);
  81. void updateTime();
  82. F32 getTotalTime() const { return mAccumTime; }
  83. F32 getDeltaTime() const { return mDt; }
  84. U32 getLastUpdateTime() const { return mLastTime; }
  85. F32 getDampness() const { return mDampness; }
  86. F32 getDampnessClamped() const { return mClampF(mDampness, 0.0, 1.0); }
  87. void setDampness(F32 dampness) { mDampness = dampness; }
  88. /// Signal used to notify systems that
  89. /// procedural shaders have been flushed.
  90. typedef Signal<void()> FlushSignal;
  91. /// Returns the signal used to notify systems that the
  92. /// procedural shaders have been flushed.
  93. FlushSignal& getFlushSignal() { return mFlushSignal; }
  94. void flushAndReInitInstances();
  95. // Flush the instance
  96. void flushInstance( BaseMaterialDefinition *target );
  97. /// Re-initializes the material instances for a specific target material.
  98. void reInitInstance( BaseMaterialDefinition *target );
  99. protected:
  100. // MatInstance tracks it's instances here
  101. friend class MatInstance;
  102. void _track(MatInstance*);
  103. void _untrack(MatInstance*);
  104. /// Flushes all the procedural shaders and re-initializes all
  105. /// the active materials instances immediately.
  106. void _flushAndReInitInstances();
  107. // Flush the instance
  108. void _flushInstance(BaseMaterialDefinition* target);
  109. /// Re-initializes the material instances for a specific target material.
  110. void _reInitInstance(BaseMaterialDefinition* target);
  111. /// @see LightManager::smActivateSignal
  112. void _onLMActivate( const char *lm, bool activate );
  113. bool _handleGFXEvent(GFXDevice::GFXDeviceEventType event);
  114. SimSet* mMaterialSet;
  115. Vector<BaseMatInstance*> mMatInstanceList;
  116. /// The default material features.
  117. FeatureSet mDefaultFeatures;
  118. /// The feature exclusion set.
  119. FeatureSet mExclusionFeatures;
  120. /// Signal used to notify systems that
  121. /// procedural shaders have been flushed.
  122. FlushSignal mFlushSignal;
  123. /// If set we flush and reinitialize all materials at the
  124. /// start of the next rendered frame.
  125. bool mFlushAndReInit;
  126. bool mMatDefShouldReload;
  127. bool mMatDefShouldFlush;
  128. // material map
  129. typedef Map<String, String> MaterialMap;
  130. MaterialMap mMaterialMap;
  131. bool mUsingDeferred;
  132. // time tracking
  133. F32 mDt;
  134. F32 mAccumTime;
  135. U32 mLastTime;
  136. F32 mDampness;
  137. BaseMatInstance* mWarningInst;
  138. BaseMaterialDefinition* mMatDefToFlush;
  139. BaseMaterialDefinition* mMatDefToReload;
  140. /// The default max anisotropy used in texture filtering.
  141. S32 mDefaultAnisotropy;
  142. /// Called when $pref::Video::defaultAnisotropy is changed.
  143. void _updateDefaultAnisotropy();
  144. /// Called when one of the feature disabling $pref::s are changed.
  145. void _onDisableMaterialFeature() { mFlushAndReInit = true; }
  146. #ifndef TORQUE_SHIPPING
  147. typedef Map<U32, BaseMatInstance *> DebugMaterialMap;
  148. DebugMaterialMap mMeshDebugMaterialInsts;
  149. #endif
  150. };
  151. /// Helper for accessing MaterialManager singleton.
  152. #define MATMGR MaterialManager::instance()
  153. #endif // _MATERIAL_MGR_H_