materialManager.h 6.5 KB

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