materialManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. #include "platform/platform.h"
  23. #include "materials/materialManager.h"
  24. #include "materials/matInstance.h"
  25. #include "materials/materialFeatureTypes.h"
  26. #include "lighting/lightManager.h"
  27. #include "core/util/safeDelete.h"
  28. #include "shaderGen/shaderGen.h"
  29. #include "core/module.h"
  30. #include "console/consoleTypes.h"
  31. #include "console/engineAPI.h"
  32. #include "gui/controls/guiTreeViewCtrl.h"
  33. MODULE_BEGIN( MaterialManager )
  34. MODULE_INIT_BEFORE( GFX )
  35. MODULE_SHUTDOWN_BEFORE( GFX )
  36. MODULE_INIT
  37. {
  38. MaterialManager::createSingleton();
  39. }
  40. MODULE_SHUTDOWN
  41. {
  42. MaterialManager::deleteSingleton();
  43. }
  44. MODULE_END;
  45. MaterialManager::MaterialManager()
  46. {
  47. VECTOR_SET_ASSOCIATION( mMatInstanceList );
  48. mDt = 0.0f;
  49. mAccumTime = 0.0f;
  50. mLastTime = 0;
  51. mDampness = 0.0f;
  52. mWarningInst = NULL;
  53. GFXDevice::getDeviceEventSignal().notify( this, &MaterialManager::_handleGFXEvent );
  54. // Make sure we get activation signals
  55. // and that we're the last to get them.
  56. LightManager::smActivateSignal.notify( this, &MaterialManager::_onLMActivate, 9999 );
  57. mMaterialSet = NULL;
  58. mUsingDeferred = false;
  59. mFlushAndReInit = false;
  60. mDefaultAnisotropy = 1;
  61. Con::addVariable( "$pref::Video::defaultAnisotropy", TypeS32, &mDefaultAnisotropy,
  62. "@brief Global variable defining the default anisotropy value.\n\n"
  63. "Controls the default anisotropic texture filtering level for all materials, including the terrain. "
  64. "This value can be changed at runtime to see its affect without reloading.\n\n "
  65. "@ingroup Materials");
  66. Con::NotifyDelegate callabck( this, &MaterialManager::_updateDefaultAnisotropy );
  67. Con::addVariableNotify( "$pref::Video::defaultAnisotropy", callabck );
  68. Con::NotifyDelegate callabck2( this, &MaterialManager::_onDisableMaterialFeature );
  69. Con::setVariable( "$pref::Video::disableNormalMapping", "false" );
  70. Con::addVariableNotify( "$pref::Video::disableNormalMapping", callabck2 );
  71. Con::setVariable( "$pref::Video::disableCubemapping", "false" );
  72. Con::addVariableNotify( "$pref::Video::disableCubemapping", callabck2 );
  73. Con::setVariable( "$pref::Video::enableParallaxMapping", "true" );
  74. Con::addVariableNotify( "$pref::Video::enableParallaxMapping", callabck2 );
  75. }
  76. MaterialManager::~MaterialManager()
  77. {
  78. GFXDevice::getDeviceEventSignal().remove( this, &MaterialManager::_handleGFXEvent );
  79. LightManager::smActivateSignal.remove( this, &MaterialManager::_onLMActivate );
  80. SAFE_DELETE( mWarningInst );
  81. #ifndef TORQUE_SHIPPING
  82. DebugMaterialMap::Iterator itr = mMeshDebugMaterialInsts.begin();
  83. for ( ; itr != mMeshDebugMaterialInsts.end(); itr++ )
  84. delete itr->value;
  85. #endif
  86. }
  87. void MaterialManager::_onLMActivate( const char *lm, bool activate )
  88. {
  89. if ( !activate )
  90. return;
  91. // Since the light manager usually swaps shadergen features
  92. // and changes system wide shader defines we need to completely
  93. // flush and rebuild all the material instances.
  94. mFlushAndReInit = true;
  95. }
  96. void MaterialManager::_updateDefaultAnisotropy()
  97. {
  98. // Update all the materials.
  99. Vector<BaseMatInstance*>::iterator iter = mMatInstanceList.begin();
  100. for ( ; iter != mMatInstanceList.end(); iter++ )
  101. (*iter)->updateStateBlocks();
  102. }
  103. Material * MaterialManager::allocateAndRegister(const String &objectName, const String &mapToName)
  104. {
  105. Material *newMat = new Material();
  106. if ( mapToName.isNotEmpty() )
  107. newMat->mMapTo = mapToName;
  108. bool registered = newMat->registerObject(objectName );
  109. AssertFatal( registered, "Unable to register material" );
  110. if (registered)
  111. Sim::getRootGroup()->addObject( newMat );
  112. else
  113. {
  114. delete newMat;
  115. newMat = NULL;
  116. }
  117. return newMat;
  118. }
  119. Material * MaterialManager::getMaterialDefinitionByName(const String &matName)
  120. {
  121. // Get the material
  122. Material * foundMat;
  123. if(!Sim::findObject(matName, foundMat))
  124. {
  125. Con::errorf("MaterialManager: Unable to find material '%s'", matName.c_str());
  126. return NULL;
  127. }
  128. return foundMat;
  129. }
  130. Material* MaterialManager::getMaterialDefinitionByMapTo(const String& mapTo)
  131. {
  132. // Get the material
  133. Material* foundMat = nullptr;
  134. for (SimSet::iterator itr = mMaterialSet->begin(); itr != mMaterialSet->end(); ++itr)
  135. {
  136. // Fetch our listed materials.
  137. Material* materialDef = dynamic_cast<Material*>(*itr);
  138. if (materialDef && materialDef->mMapTo.compare(mapTo, 0U, String::NoCase) == 0)
  139. {
  140. //We have a match, so keep it and bail the loop
  141. foundMat = materialDef;
  142. break;
  143. }
  144. }
  145. return foundMat;
  146. }
  147. BaseMatInstance* MaterialManager::createMatInstance(const String &matName)
  148. {
  149. BaseMaterialDefinition* mat = NULL;
  150. if (Sim::findObject(matName, mat))
  151. return mat->createMatInstance();
  152. return NULL;
  153. }
  154. BaseMatInstance* MaterialManager::createMatInstance( const String &matName,
  155. const GFXVertexFormat *vertexFormat )
  156. {
  157. return createMatInstance( matName, getDefaultFeatures(), vertexFormat );
  158. }
  159. BaseMatInstance* MaterialManager::createMatInstance( const String &matName,
  160. const FeatureSet& features,
  161. const GFXVertexFormat *vertexFormat )
  162. {
  163. BaseMatInstance* mat = createMatInstance(matName);
  164. if (mat)
  165. {
  166. mat->init( features, vertexFormat );
  167. return mat;
  168. }
  169. return NULL;
  170. }
  171. BaseMatInstance * MaterialManager::createWarningMatInstance()
  172. {
  173. Material *warnMat = static_cast<Material*>(Sim::findObject("WarningMaterial"));
  174. BaseMatInstance *warnMatInstance = NULL;
  175. if( warnMat != NULL )
  176. {
  177. warnMatInstance = warnMat->createMatInstance();
  178. GFXStateBlockDesc desc;
  179. desc.setCullMode(GFXCullNone);
  180. warnMatInstance->addStateBlockDesc(desc);
  181. warnMatInstance->init( getDefaultFeatures(),
  182. getGFXVertexFormat<GFXVertexPNTTB>() );
  183. }
  184. else
  185. Con::errorf("WarningMaterial Not Found!");
  186. return warnMatInstance;
  187. }
  188. // Gets the global warning material instance, callers should not free this copy
  189. BaseMatInstance * MaterialManager::getWarningMatInstance()
  190. {
  191. if (!mWarningInst)
  192. mWarningInst = createWarningMatInstance();
  193. return mWarningInst;
  194. }
  195. #ifndef TORQUE_SHIPPING
  196. BaseMatInstance * MaterialManager::createMeshDebugMatInstance(const LinearColorF &meshColor)
  197. {
  198. String meshDebugStr = String::ToString( "Torque_MeshDebug_%d", meshColor.getRGBAPack() );
  199. Material *debugMat;
  200. if (!Sim::findObject(meshDebugStr,debugMat))
  201. {
  202. debugMat = allocateAndRegister( meshDebugStr );
  203. debugMat->mDiffuse[0] = meshColor;
  204. debugMat->mEmissive[0] = true;
  205. }
  206. BaseMatInstance *debugMatInstance = NULL;
  207. if( debugMat != NULL )
  208. {
  209. debugMatInstance = debugMat->createMatInstance();
  210. GFXStateBlockDesc desc;
  211. desc.setCullMode(GFXCullNone);
  212. desc.fillMode = GFXFillWireframe;
  213. debugMatInstance->addStateBlockDesc(desc);
  214. // Disable fog and other stuff.
  215. FeatureSet debugFeatures;
  216. debugFeatures.addFeature( MFT_DiffuseColor );
  217. debugMatInstance->init( debugFeatures, getGFXVertexFormat<GFXVertexPCN>() );
  218. }
  219. return debugMatInstance;
  220. }
  221. // Gets the global material instance for a given color, callers should not free this copy
  222. BaseMatInstance *MaterialManager::getMeshDebugMatInstance(const LinearColorF &meshColor)
  223. {
  224. DebugMaterialMap::Iterator itr = mMeshDebugMaterialInsts.find( meshColor.getRGBAPack() );
  225. BaseMatInstance *inst = NULL;
  226. if ( itr == mMeshDebugMaterialInsts.end() )
  227. inst = createMeshDebugMatInstance( meshColor );
  228. else
  229. inst = itr->value;
  230. mMeshDebugMaterialInsts.insert( meshColor.getRGBAPack(), inst );
  231. return inst;
  232. }
  233. #endif
  234. void MaterialManager::mapMaterial(const String & textureName, const String & materialName)
  235. {
  236. if (getMapEntry(textureName).isNotEmpty())
  237. {
  238. if (!textureName.equal("unmapped_mat", String::NoCase))
  239. Con::warnf(ConsoleLogEntry::General, "Warning, overwriting material for: %s", textureName.c_str());
  240. }
  241. mMaterialMap[String::ToLower(textureName)] = materialName;
  242. }
  243. String MaterialManager::getMapEntry(const String & textureName) const
  244. {
  245. MaterialMap::ConstIterator iter = mMaterialMap.find(String::ToLower(textureName));
  246. if ( iter == mMaterialMap.end() )
  247. return String();
  248. return iter->value;
  249. }
  250. void MaterialManager::flushAndReInitInstances()
  251. {
  252. // Clear the flag if its set.
  253. mFlushAndReInit = false;
  254. // Check to see if any shader preferences have changed.
  255. recalcFeaturesFromPrefs();
  256. // First we flush all the shader gen shaders which will
  257. // invalidate all GFXShader* to them.
  258. SHADERGEN->flushProceduralShaders();
  259. mFlushSignal.trigger();
  260. // First do a pass deleting all hooks as they can contain
  261. // materials themselves. This means we have to restart the
  262. // loop every time we delete any hooks... lame.
  263. Vector<BaseMatInstance*>::iterator iter = mMatInstanceList.begin();
  264. while ( iter != mMatInstanceList.end() )
  265. {
  266. if ( (*iter)->deleteAllHooks() != 0 )
  267. {
  268. // Restart the loop.
  269. iter = mMatInstanceList.begin();
  270. continue;
  271. }
  272. iter++;
  273. }
  274. // Now do a pass re-initializing materials.
  275. iter = mMatInstanceList.begin();
  276. for ( ; iter != mMatInstanceList.end(); iter++ )
  277. (*iter)->reInit();
  278. }
  279. // Used in the materialEditor. This flushes the material preview object so it can be reloaded easily.
  280. void MaterialManager::flushInstance( BaseMaterialDefinition *target )
  281. {
  282. Vector<BaseMatInstance*>::iterator iter = mMatInstanceList.begin();
  283. while ( iter != mMatInstanceList.end() )
  284. {
  285. if ( (*iter)->getMaterial() == target )
  286. {
  287. (*iter)->deleteAllHooks();
  288. return;
  289. }
  290. iter++;
  291. }
  292. }
  293. void MaterialManager::reInitInstance( BaseMaterialDefinition *target )
  294. {
  295. Vector<BaseMatInstance*>::iterator iter = mMatInstanceList.begin();
  296. for ( ; iter != mMatInstanceList.end(); iter++ )
  297. {
  298. if ( (*iter)->getMaterial() == target )
  299. (*iter)->reInit();
  300. }
  301. }
  302. void MaterialManager::updateTime()
  303. {
  304. U32 curTime = Sim::getCurrentTime();
  305. if(curTime > mLastTime)
  306. {
  307. mDt = (curTime - mLastTime) / 1000.0f;
  308. mLastTime = curTime;
  309. mAccumTime += mDt;
  310. }
  311. else
  312. mDt = 0.0f;
  313. }
  314. SimSet * MaterialManager::getMaterialSet()
  315. {
  316. if(!mMaterialSet)
  317. mMaterialSet = static_cast<SimSet*>( Sim::findObject( "MaterialSet" ) );
  318. AssertFatal( mMaterialSet, "MaterialSet not found" );
  319. return mMaterialSet;
  320. }
  321. void MaterialManager::dumpMaterialInstances( BaseMaterialDefinition *target ) const
  322. {
  323. if ( !mMatInstanceList.size() )
  324. return;
  325. if ( target )
  326. Con::printf( "--------------------- %s MatInstances ---------------------", target->getName() );
  327. else
  328. Con::printf( "--------------------- MatInstances %d ---------------------", mMatInstanceList.size() );
  329. for( U32 i=0; i<mMatInstanceList.size(); i++ )
  330. {
  331. BaseMatInstance *inst = mMatInstanceList[i];
  332. if ( target && inst->getMaterial() != target )
  333. continue;
  334. inst->dumpShaderInfo();
  335. Con::printf( "" );
  336. }
  337. Con::printf( "---------------------- Dump complete ----------------------");
  338. }
  339. void MaterialManager::getMaterialInstances(BaseMaterialDefinition* target, GuiTreeViewCtrl* materailInstanceTree)
  340. {
  341. if (!mMatInstanceList.size())
  342. return;
  343. if (!target)
  344. {
  345. Con::errorf("Can't form a list without a specific MaterialDefinition");
  346. return;
  347. }
  348. if (!materailInstanceTree)
  349. {
  350. Con::errorf("Requires a valid GuiTreeViewCtrl object to populate data into!");
  351. return;
  352. }
  353. U32 matItem = materailInstanceTree->insertItem(0, target->getName());
  354. for (U32 i = 0; i < mMatInstanceList.size(); i++)
  355. {
  356. BaseMatInstance* inst = mMatInstanceList[i];
  357. if (target && inst->getMaterial() != target)
  358. continue;
  359. inst->getShaderInfo(materailInstanceTree, matItem);
  360. }
  361. }
  362. void MaterialManager::_track( MatInstance *matInstance )
  363. {
  364. mMatInstanceList.push_back( matInstance );
  365. }
  366. void MaterialManager::_untrack( MatInstance *matInstance )
  367. {
  368. mMatInstanceList.remove( matInstance );
  369. }
  370. void MaterialManager::recalcFeaturesFromPrefs()
  371. {
  372. mDefaultFeatures.clear();
  373. FeatureType::addDefaultTypes( &mDefaultFeatures );
  374. mExclusionFeatures.setFeature( MFT_NormalMap,
  375. Con::getBoolVariable( "$pref::Video::disableNormalMapping", false ) );
  376. mExclusionFeatures.setFeature( MFT_CubeMap,
  377. Con::getBoolVariable( "$pref::Video::disableCubemapping", false ) );
  378. mExclusionFeatures.setFeature( MFT_Parallax,
  379. !Con::getBoolVariable( "$pref::Video::enableParallaxMapping", true ) );
  380. }
  381. bool MaterialManager::_handleGFXEvent( GFXDevice::GFXDeviceEventType event_ )
  382. {
  383. switch ( event_ )
  384. {
  385. case GFXDevice::deInit:
  386. recalcFeaturesFromPrefs();
  387. break;
  388. case GFXDevice::deDestroy :
  389. SAFE_DELETE( mWarningInst );
  390. break;
  391. case GFXDevice::deStartOfFrame:
  392. if ( mFlushAndReInit )
  393. flushAndReInitInstances();
  394. break;
  395. default:
  396. break;
  397. }
  398. return true;
  399. }
  400. DefineEngineFunction( reInitMaterials, void, (),,
  401. "@brief Flushes all procedural shaders and re-initializes all active material instances.\n\n"
  402. "@ingroup Materials")
  403. {
  404. MATMGR->flushAndReInitInstances();
  405. }
  406. DefineEngineFunction( addMaterialMapping, void, (const char * texName, const char * matName), , "(string texName, string matName)\n"
  407. "@brief Maps the given texture to the given material.\n\n"
  408. "Generates a console warning before overwriting.\n\n"
  409. "Material maps are used by terrain and interiors for triggering "
  410. "effects when an object moves onto a terrain "
  411. "block or interior surface using the associated texture.\n\n"
  412. "@ingroup Materials")
  413. {
  414. MATMGR->mapMaterial(texName, matName);
  415. }
  416. DefineEngineFunction( getMaterialMapping, const char*, (const char * texName), , "(string texName)\n"
  417. "@brief Returns the name of the material mapped to this texture.\n\n"
  418. "If no materials are found, an empty string is returned.\n\n"
  419. "@param texName Name of the texture\n\n"
  420. "@ingroup Materials")
  421. {
  422. return MATMGR->getMapEntry(texName).c_str();
  423. }
  424. DefineEngineFunction( dumpMaterialInstances, void, (), ,
  425. "@brief Dumps a formatted list of currently allocated material instances to the console.\n\n"
  426. "@ingroup Materials")
  427. {
  428. MATMGR->dumpMaterialInstances();
  429. }
  430. DefineEngineFunction(getMaterialInstances, void, (BaseMaterialDefinition* target, GuiTreeViewCtrl* tree), (nullAsType<BaseMaterialDefinition*>(), nullAsType<GuiTreeViewCtrl*>()),
  431. "@brief Dumps a formatted list of currently allocated material instances to the console.\n\n"
  432. "@ingroup Materials")
  433. {
  434. if (target == nullptr || tree == nullptr)
  435. return;
  436. MATMGR->getMaterialInstances(target, tree);
  437. }
  438. DefineEngineFunction( getMapEntry, const char*, (const char * texName), ,
  439. "@hide")
  440. {
  441. return MATMGR->getMapEntry( String(texName) );
  442. }