materialManager.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /// Flushes all the procedural shaders and re-initializes all
  95. /// the active materials instances immediately.
  96. void flushAndReInitInstances();
  97. // Flush the instance
  98. void flushInstance( BaseMaterialDefinition *target );
  99. /// Re-initializes the material instances for a specific target material.
  100. void reInitInstance( BaseMaterialDefinition *target );
  101. protected:
  102. // MatInstance tracks it's instances here
  103. friend class MatInstance;
  104. void _track(MatInstance*);
  105. void _untrack(MatInstance*);
  106. /// @see LightManager::smActivateSignal
  107. void _onLMActivate( const char *lm, bool activate );
  108. bool _handleGFXEvent(GFXDevice::GFXDeviceEventType event);
  109. SimSet* mMaterialSet;
  110. Vector<BaseMatInstance*> mMatInstanceList;
  111. /// The default material features.
  112. FeatureSet mDefaultFeatures;
  113. /// The feature exclusion set.
  114. FeatureSet mExclusionFeatures;
  115. /// Signal used to notify systems that
  116. /// procedural shaders have been flushed.
  117. FlushSignal mFlushSignal;
  118. /// If set we flush and reinitialize all materials at the
  119. /// start of the next rendered frame.
  120. bool mFlushAndReInit;
  121. // material map
  122. typedef Map<String, String> MaterialMap;
  123. MaterialMap mMaterialMap;
  124. bool mUsingDeferred;
  125. // time tracking
  126. F32 mDt;
  127. F32 mAccumTime;
  128. U32 mLastTime;
  129. F32 mDampness;
  130. BaseMatInstance* mWarningInst;
  131. /// The default max anisotropy used in texture filtering.
  132. S32 mDefaultAnisotropy;
  133. /// Called when $pref::Video::defaultAnisotropy is changed.
  134. void _updateDefaultAnisotropy();
  135. /// Called when one of the feature disabling $pref::s are changed.
  136. void _onDisableMaterialFeature() { mFlushAndReInit = true; }
  137. #ifndef TORQUE_SHIPPING
  138. typedef Map<U32, BaseMatInstance *> DebugMaterialMap;
  139. DebugMaterialMap mMeshDebugMaterialInsts;
  140. #endif
  141. };
  142. /// Helper for accessing MaterialManager singleton.
  143. #define MATMGR MaterialManager::instance()
  144. #endif // _MATERIAL_MGR_H_