materialManager.h 6.3 KB

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