/* * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or * its licensors. * * For complete copyright and license terms please see the LICENSE at the root of this * distribution (the "License"). All use of this software is governed by the License, * or, if provided, by the license below or the license accompanying this file. Do not * remove or modify any license notices. This file is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * */ // Original file Copyright Crytek GMBH or its affiliates, used under license. #ifndef CRYINCLUDE_CRY3DENGINE_MATMAN_H #define CRYINCLUDE_CRY3DENGINE_MATMAN_H #pragma once #include "Cry3DEngineBase.h" #include "SurfaceTypeManager.h" #include #include "MaterialHelpers.h" #include "ManualResetEvent.h" #include "UniqueManualEvent.h" #include // forward declarations. struct IMaterial; struct ISurfaceType; struct ISurfaceTypeManager; class CMatInfo; ////////////////////////////////////////////////////////////////////////// // // CMatMan is a material manager class. // ////////////////////////////////////////////////////////////////////////// class CMatMan : public IMaterialManager , public Cry3DEngineBase , public AzFramework::LegacyAssetEventBus::Handler { public: CMatMan(); virtual ~CMatMan(); void ShutDown(); // interface IMaterialManager -------------------------------------------------------- virtual _smart_ptr CreateMaterial(const char* sMtlName, int nMtlFlags = 0); virtual _smart_ptr FindMaterial(const char* sMtlName) const; virtual _smart_ptr LoadMaterial(const char* sMtlName, bool bMakeIfNotFound = true, bool bNonremovable = false, unsigned long nLoadingFlags = 0); virtual _smart_ptr LoadMaterialFromXml(const char* sMtlName, XmlNodeRef mtlNode); virtual void ReloadMaterial(_smart_ptr pMtl); virtual void SetListener(IMaterialManagerListener* pListener) { m_pListener = pListener; }; virtual _smart_ptr GetDefaultMaterial(); virtual _smart_ptr GetDefaultTerrainLayerMaterial() { if (!m_bInitialized) { InitDefaults(); } return m_pDefaultTerrainLayersMtl; } virtual _smart_ptr GetDefaultLayersMaterial(); virtual _smart_ptr GetDefaultHelperMaterial(); virtual ISurfaceType* GetSurfaceTypeByName(const char* sSurfaceTypeName, const char* sWhy = NULL); virtual int GetSurfaceTypeIdByName(const char* sSurfaceTypeName, const char* sWhy = NULL); virtual ISurfaceType* GetSurfaceType(int nSurfaceTypeId, const char* sWhy = NULL) { return m_pSurfaceTypeManager->GetSurfaceTypeFast(nSurfaceTypeId, sWhy); } virtual ISurfaceTypeManager* GetSurfaceTypeManager() { return m_pSurfaceTypeManager; } _smart_ptr LoadCGFMaterial(CMaterialCGF* pMaterialCGF, const char* sCgfFilename, unsigned long nLoadingFlags = 0) override; virtual _smart_ptr CloneMaterial(_smart_ptr pMtl, int nSubMtl = -1); virtual _smart_ptr CloneMultiMaterial(_smart_ptr pMtl, const char* sSubMtlName = 0); virtual void GetLoadedMaterials(AZStd::vector<_smart_ptr>* pData, uint32& nObjCount) const; virtual bool SaveMaterial(XmlNodeRef mtlNode, _smart_ptr pMtl); virtual void CopyMaterial(_smart_ptr pMtlSrc, _smart_ptr pMtlDest, EMaterialCopyFlags flags); virtual void RenameMaterial(_smart_ptr pMtl, const char* sNewName); virtual void RefreshMaterialRuntime(); // ------------------------------------------------------------------------------------ void InitDefaults(); void PreloadLevelMaterials(); void DoLoadSurfaceTypesInInit(bool doLoadSurfaceTypesInInit); void UpdateShaderItems(); void RefreshShaderResourceConstants(); // Load all known game decal materials. void PreloadDecalMaterials(); void SetSketchMode(int mode); int GetSketchMode() { return e_sketch_mode; } void SetTexelDensityDebug(int mode); int GetTexelDensityDebug() { return e_texeldensity; } ////////////////////////////////////////////////////////////////////////// ISurfaceType* GetSurfaceTypeFast(int nSurfaceTypeId, const char* sWhy = NULL) { return m_pSurfaceTypeManager->GetSurfaceTypeFast(nSurfaceTypeId, sWhy); } virtual void GetMemoryUsage(ICrySizer* pSizer) const; private: // ----------------------------------------------------------------------------- friend class CMatInfo; bool Unregister(_smart_ptr pMat, bool deleteEditorMaterial = true); _smart_ptr CreateMaterialPlaceholder(const char* materialName, int nMtlFlags, const char* textureName, _smart_ptr existingMtl = nullptr); bool LoadMaterialShader(_smart_ptr pMtl, _smart_ptr pParentMtl, const char* sShader, uint64 nShaderGenMask, SInputShaderResources& sr, XmlNodeRef& publicsNode); bool LoadMaterialLayerSlot(uint32 nSlot, _smart_ptr pMtl, const char* szShaderName, SInputShaderResources& pBaseResources, XmlNodeRef& pPublicsNode, uint8 nLayerFlags); void ParsePublicParams(SInputShaderResources& sr, XmlNodeRef paramsNode); AZStd::string UnifyName(const char* sMtlName) const; // Can be called after material creation and initialization, to inform editor that new material in engine exist. // Only used internally. void NotifyCreateMaterial(_smart_ptr pMtl); // Make a valid material from the XML node. _smart_ptr MakeMaterialFromXml(const AZStd::string& sMtlName, XmlNodeRef node, bool bForcePureChild, uint16 sortPrio = 0, _smart_ptr pExistingMtl = 0, unsigned long nLoadingFlags = 0, _smart_ptr pParentMtl = 0); template UniqueManualEvent CheckMaterialCache(const AZStd::string& name, T& cachedMaterial); _smart_ptr LoadMaterialInternal(const char* sMtlName, bool bMakeIfNotFound, bool bNonremovable, unsigned long nLoadingFlags); // override from LegacyAssetEventBus::Handler // Notifies listeners that a file changed void OnFileChanged(AZStd::string assetPath) override; void OnFileRemoved(AZStd::string assetPath) override; private: typedef AZStd::unordered_map > MtlNameMap; MtlNameMap m_mtlNameMap; // IMaterialManagerListener* m_pListener; // _smart_ptr m_pDefaultMtl; // _smart_ptr m_pDefaultLayersMtl; // _smart_ptr m_pDefaultTerrainLayersMtl; // _smart_ptr m_pNoDrawMtl; // _smart_ptr m_pDefaultHelperMtl; std::vector<_smart_ptr > m_nonRemovables; // CSurfaceTypeManager* m_pSurfaceTypeManager; // ////////////////////////////////////////////////////////////////////////// // Cached XML parser. _smart_ptr m_pXmlParser; bool m_bInitialized; bool m_bLoadSurfaceTypesInInit; mutable AZStd::mutex m_nonRemovablesMutex; mutable AZStd::recursive_mutex m_materialMapMutex; AZStd::unordered_map> m_pendingMaterialLoads; public: // Global namespace "instance", not a class "instance", no member-variables, only const functions; // Used to encapsulate the material-definition/io into Cry3DEngine (and make it plugable that way). static MaterialHelpers s_materialHelpers; static int e_sketch_mode; static int e_lowspec_mode; static int e_pre_sketch_spec; static int e_texeldensity; }; #endif // CRYINCLUDE_CRY3DENGINE_MATMAN_H