materialManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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->mReceiveShadows[0] = false;
  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. String currentMapEntry = getMapEntry(textureName);
  237. if (currentMapEntry.isNotEmpty())
  238. {
  239. if (!textureName.equal("unmapped_mat", String::NoCase))
  240. {
  241. SimObject* originalMat;
  242. SimObject* newMat;
  243. if (Sim::findObject(currentMapEntry, originalMat) && Sim::findObject(materialName, newMat))
  244. Con::warnf(ConsoleLogEntry::General, "Warning, overwriting material for: \"%s\" in %s by %s", textureName.c_str(), originalMat->getFilename(), newMat->getFilename());
  245. else
  246. Con::warnf(ConsoleLogEntry::General, "Warning, overwriting material for: %s", textureName.c_str());
  247. }
  248. }
  249. mMaterialMap[String::ToLower(textureName)] = materialName;
  250. }
  251. String MaterialManager::getMapEntry(const String & textureName) const
  252. {
  253. MaterialMap::ConstIterator iter = mMaterialMap.find(String::ToLower(textureName));
  254. if ( iter == mMaterialMap.end() )
  255. return String();
  256. return iter->value;
  257. }
  258. void MaterialManager::flushAndReInitInstances()
  259. {
  260. // Clear the flag if its set.
  261. mFlushAndReInit = false;
  262. // Check to see if any shader preferences have changed.
  263. recalcFeaturesFromPrefs();
  264. // First we flush all the shader gen shaders which will
  265. // invalidate all GFXShader* to them.
  266. SHADERGEN->flushProceduralShaders();
  267. mFlushSignal.trigger();
  268. // First do a pass deleting all hooks as they can contain
  269. // materials themselves. This means we have to restart the
  270. // loop every time we delete any hooks... lame.
  271. Vector<BaseMatInstance*>::iterator iter = mMatInstanceList.begin();
  272. while ( iter != mMatInstanceList.end() )
  273. {
  274. if ( (*iter)->deleteAllHooks() != 0 )
  275. {
  276. // Restart the loop.
  277. iter = mMatInstanceList.begin();
  278. continue;
  279. }
  280. iter++;
  281. }
  282. // Now do a pass re-initializing materials.
  283. iter = mMatInstanceList.begin();
  284. for ( ; iter != mMatInstanceList.end(); iter++ )
  285. (*iter)->reInit();
  286. }
  287. void MaterialManager::flushInstance( BaseMaterialDefinition *target )
  288. {
  289. Vector<BaseMatInstance*>::iterator iter = mMatInstanceList.begin();
  290. while ( iter != mMatInstanceList.end() )
  291. {
  292. if ( (*iter)->getMaterial() == target )
  293. {
  294. (*iter)->deleteAllHooks();
  295. return;
  296. }
  297. iter++;
  298. }
  299. }
  300. void MaterialManager::reInitInstance( BaseMaterialDefinition *target )
  301. {
  302. Vector<BaseMatInstance*>::iterator iter = mMatInstanceList.begin();
  303. for ( ; iter != mMatInstanceList.end(); iter++ )
  304. {
  305. if ( (*iter)->getMaterial() == target )
  306. (*iter)->reInit();
  307. }
  308. }
  309. void MaterialManager::updateTime()
  310. {
  311. U32 curTime = Sim::getCurrentTime();
  312. if(curTime > mLastTime)
  313. {
  314. mDt = (curTime - mLastTime) / 1000.0f;
  315. mLastTime = curTime;
  316. mAccumTime += mDt;
  317. }
  318. else
  319. mDt = 0.0f;
  320. }
  321. SimSet * MaterialManager::getMaterialSet()
  322. {
  323. if(!mMaterialSet)
  324. mMaterialSet = static_cast<SimSet*>( Sim::findObject( "MaterialSet" ) );
  325. AssertFatal( mMaterialSet, "MaterialSet not found" );
  326. return mMaterialSet;
  327. }
  328. void MaterialManager::dumpMaterialInstances( BaseMaterialDefinition *target ) const
  329. {
  330. if ( !mMatInstanceList.size() )
  331. return;
  332. if ( target )
  333. Con::printf( "--------------------- %s MatInstances ---------------------", target->getName() );
  334. else
  335. Con::printf( "--------------------- MatInstances %d ---------------------", mMatInstanceList.size() );
  336. for( U32 i=0; i<mMatInstanceList.size(); i++ )
  337. {
  338. BaseMatInstance *inst = mMatInstanceList[i];
  339. if ( target && inst->getMaterial() != target )
  340. continue;
  341. inst->dumpShaderInfo();
  342. Con::printf( "" );
  343. }
  344. Con::printf( "---------------------- Dump complete ----------------------");
  345. }
  346. void MaterialManager::getMaterialInstances(BaseMaterialDefinition* target, GuiTreeViewCtrl* materailInstanceTree)
  347. {
  348. if (!mMatInstanceList.size())
  349. return;
  350. if (!target)
  351. {
  352. Con::errorf("Can't form a list without a specific MaterialDefinition");
  353. return;
  354. }
  355. if (!materailInstanceTree)
  356. {
  357. Con::errorf("Requires a valid GuiTreeViewCtrl object to populate data into!");
  358. return;
  359. }
  360. U32 matItem = materailInstanceTree->insertItem(0, target->getName());
  361. for (U32 i = 0; i < mMatInstanceList.size(); i++)
  362. {
  363. BaseMatInstance* inst = mMatInstanceList[i];
  364. if (target && inst->getMaterial() != target)
  365. continue;
  366. inst->getShaderInfo(materailInstanceTree, matItem);
  367. }
  368. }
  369. void MaterialManager::_track( MatInstance *matInstance )
  370. {
  371. mMatInstanceList.push_back( matInstance );
  372. }
  373. void MaterialManager::_untrack( MatInstance *matInstance )
  374. {
  375. mMatInstanceList.remove( matInstance );
  376. }
  377. void MaterialManager::recalcFeaturesFromPrefs()
  378. {
  379. mDefaultFeatures.clear();
  380. FeatureType::addDefaultTypes( &mDefaultFeatures );
  381. mExclusionFeatures.setFeature( MFT_NormalMap,
  382. Con::getBoolVariable( "$pref::Video::disableNormalMapping", false ) );
  383. mExclusionFeatures.setFeature( MFT_CubeMap,
  384. Con::getBoolVariable( "$pref::Video::disableCubemapping", false ) );
  385. mExclusionFeatures.setFeature( MFT_Parallax,
  386. !Con::getBoolVariable( "$pref::Video::enableParallaxMapping", true ) );
  387. }
  388. bool MaterialManager::_handleGFXEvent( GFXDevice::GFXDeviceEventType event_ )
  389. {
  390. switch ( event_ )
  391. {
  392. case GFXDevice::deInit:
  393. recalcFeaturesFromPrefs();
  394. break;
  395. case GFXDevice::deDestroy :
  396. SAFE_DELETE( mWarningInst );
  397. break;
  398. case GFXDevice::deStartOfFrame:
  399. if ( mFlushAndReInit )
  400. flushAndReInitInstances();
  401. break;
  402. default:
  403. break;
  404. }
  405. return true;
  406. }
  407. DefineEngineFunction( reInitMaterials, void, (),,
  408. "@brief Flushes all procedural shaders and re-initializes all active material instances.\n\n"
  409. "@ingroup Materials")
  410. {
  411. MATMGR->flushAndReInitInstances();
  412. }
  413. DefineEngineFunction( addMaterialMapping, void, (const char * texName, const char * matName), , "(string texName, string matName)\n"
  414. "@brief Maps the given texture to the given material.\n\n"
  415. "Generates a console warning before overwriting.\n\n"
  416. "Material maps are used by terrain and interiors for triggering "
  417. "effects when an object moves onto a terrain "
  418. "block or interior surface using the associated texture.\n\n"
  419. "@ingroup Materials")
  420. {
  421. MATMGR->mapMaterial(texName, matName);
  422. }
  423. DefineEngineFunction( getMaterialMapping, const char*, (const char * texName), , "(string texName)\n"
  424. "@brief Returns the name of the material mapped to this texture.\n\n"
  425. "If no materials are found, an empty string is returned.\n\n"
  426. "@param texName Name of the texture\n\n"
  427. "@ingroup Materials")
  428. {
  429. return MATMGR->getMapEntry(texName).c_str();
  430. }
  431. DefineEngineFunction( dumpMaterialInstances, void, (), ,
  432. "@brief Dumps a formatted list of currently allocated material instances to the console.\n\n"
  433. "@ingroup Materials")
  434. {
  435. MATMGR->dumpMaterialInstances();
  436. }
  437. DefineEngineFunction(getMaterialInstances, void, (BaseMaterialDefinition* target, GuiTreeViewCtrl* tree), (nullAsType<BaseMaterialDefinition*>(), nullAsType<GuiTreeViewCtrl*>()),
  438. "@brief Dumps a formatted list of currently allocated material instances to the console.\n\n"
  439. "@ingroup Materials")
  440. {
  441. if (target == nullptr || tree == nullptr)
  442. return;
  443. MATMGR->getMaterialInstances(target, tree);
  444. }
  445. DefineEngineFunction( getMapEntry, const char*, (const char * texName), ,
  446. "@hide")
  447. {
  448. return MATMGR->getMapEntry( String(texName) );
  449. }