materialManager.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. SimSet * getMaterialSet();
  49. // map textures to materials
  50. void mapMaterial(const String & textureName, const String & materialName);
  51. String getMapEntry(const String & textureName) const;
  52. // Return instance of named material caller is responsible for memory
  53. BaseMatInstance * createMatInstance( const String &matName );
  54. // Create a BaseMatInstance with the default feature flags.
  55. BaseMatInstance * createMatInstance( const String &matName, const GFXVertexFormat *vertexFormat );
  56. BaseMatInstance * createMatInstance( const String &matName, const FeatureSet &features, const GFXVertexFormat *vertexFormat );
  57. /// The default feature set for materials.
  58. const FeatureSet& getDefaultFeatures() const { return mDefaultFeatures; }
  59. /// The feature exclusion list for disabling features.
  60. const FeatureSet& getExclusionFeatures() const { return mExclusionFeatures; }
  61. void recalcFeaturesFromPrefs();
  62. /// Get the default texture anisotropy.
  63. U32 getDefaultAnisotropy() const { return mDefaultAnisotropy; }
  64. /// Allocate and return an instance of special materials. Caller is responsible for the memory.
  65. BaseMatInstance * createWarningMatInstance();
  66. /// Gets the global warning material instance, callers should not free this copy
  67. BaseMatInstance * getWarningMatInstance();
  68. /// Set the deferred enabled state.
  69. void setDeferredEnabled( bool enabled ) { mUsingDeferred = enabled; }
  70. /// Get the deferred enabled state.
  71. bool getDeferredEnabled() const { return mUsingDeferred; }
  72. #ifndef TORQUE_SHIPPING
  73. // Allocate and return an instance of mesh debugging materials. Caller is responsible for the memory.
  74. BaseMatInstance * createMeshDebugMatInstance(const LinearColorF &meshColor);
  75. // Gets the global material instance for a given color, callers should not free this copy
  76. BaseMatInstance * getMeshDebugMatInstance(const LinearColorF &meshColor);
  77. #endif
  78. void dumpMaterialInstances( BaseMaterialDefinition *target = NULL ) const;
  79. void getMaterialInstances(BaseMaterialDefinition* target, GuiTreeViewCtrl* tree);
  80. void updateTime();
  81. F32 getTotalTime() const { return mAccumTime; }
  82. F32 getDeltaTime() const { return mDt; }
  83. U32 getLastUpdateTime() const { return mLastTime; }
  84. /// Signal used to notify systems that
  85. /// procedural shaders have been flushed.
  86. typedef Signal<void()> FlushSignal;
  87. /// Returns the signal used to notify systems that the
  88. /// procedural shaders have been flushed.
  89. FlushSignal& getFlushSignal() { return mFlushSignal; }
  90. /// Flushes all the procedural shaders and re-initializes all
  91. /// the active materials instances immediately.
  92. void flushAndReInitInstances();
  93. // Flush the instance
  94. void flushInstance( BaseMaterialDefinition *target );
  95. /// Re-initializes the material instances for a specific target material.
  96. void reInitInstance( BaseMaterialDefinition *target );
  97. protected:
  98. // MatInstance tracks it's instances here
  99. friend class MatInstance;
  100. void _track(MatInstance*);
  101. void _untrack(MatInstance*);
  102. /// @see LightManager::smActivateSignal
  103. void _onLMActivate( const char *lm, bool activate );
  104. bool _handleGFXEvent(GFXDevice::GFXDeviceEventType event);
  105. SimSet* mMaterialSet;
  106. Vector<BaseMatInstance*> mMatInstanceList;
  107. /// The default material features.
  108. FeatureSet mDefaultFeatures;
  109. /// The feature exclusion set.
  110. FeatureSet mExclusionFeatures;
  111. /// Signal used to notify systems that
  112. /// procedural shaders have been flushed.
  113. FlushSignal mFlushSignal;
  114. /// If set we flush and reinitialize all materials at the
  115. /// start of the next rendered frame.
  116. bool mFlushAndReInit;
  117. // material map
  118. typedef Map<String, String> MaterialMap;
  119. MaterialMap mMaterialMap;
  120. bool mUsingDeferred;
  121. // time tracking
  122. F32 mDt;
  123. F32 mAccumTime;
  124. U32 mLastTime;
  125. BaseMatInstance* mWarningInst;
  126. /// The default max anisotropy used in texture filtering.
  127. S32 mDefaultAnisotropy;
  128. /// Called when $pref::Video::defaultAnisotropy is changed.
  129. void _updateDefaultAnisotropy();
  130. /// Called when one of the feature disabling $pref::s are changed.
  131. void _onDisableMaterialFeature() { mFlushAndReInit = true; }
  132. #ifndef TORQUE_SHIPPING
  133. typedef Map<U32, BaseMatInstance *> DebugMaterialMap;
  134. DebugMaterialMap mMeshDebugMaterialInsts;
  135. #endif
  136. };
  137. /// Helper for accessing MaterialManager singleton.
  138. #define MATMGR MaterialManager::instance()
  139. #endif // _MATERIAL_MGR_H_