Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_SHADERMANAGER 19 : #define INCLUDED_SHADERMANAGER 20 : 21 : #include "graphics/ShaderDefines.h" 22 : #include "graphics/ShaderProgramPtr.h" 23 : #include "graphics/ShaderTechnique.h" 24 : #include "graphics/ShaderTechniquePtr.h" 25 : #include "lib/file/vfs/vfs_path.h" 26 : #include "lib/path.h" 27 : #include "lib/status.h" 28 : #include "ps/CStr.h" 29 : #include "ps/CStrIntern.h" 30 : 31 : #include <cstddef> 32 : #include <map> 33 : #include <memory> 34 : #include <set> 35 : #include <string> 36 : #include <unordered_map> 37 : 38 : class CShaderProgram; 39 : namespace Renderer::Backend { class IDevice; } 40 : 41 : /** 42 : * Shader manager: loads and caches shader programs. 43 : * 44 : * For a high-level overview of shaders and materials, see 45 : * https://gitea.wildfiregames.com/0ad/0ad/wiki/MaterialSystem 46 : */ 47 : class CShaderManager 48 : { 49 : public: 50 : CShaderManager(Renderer::Backend::IDevice* device); 51 : ~CShaderManager(); 52 : 53 : /** 54 : * Load a shader effect. 55 : * Effects can be implemented via many techniques; this returns the best usable technique. 56 : * @param name name of effect XML specification (file is loaded from shaders/effects/${name}.xml) 57 : * @param defines key/value set of preprocessor definitions 58 : * @return loaded technique, or empty technique on error 59 : */ 60 : CShaderTechniquePtr LoadEffect(CStrIntern name, const CShaderDefines& defines); 61 : 62 : /** 63 : * Load a shader effect, with empty defines. 64 : */ 65 : CShaderTechniquePtr LoadEffect(CStrIntern name); 66 : 67 : /** 68 : * Load a shader effect with the pipeline state description overwriting. 69 : * TODO: we should set all needed states in XML. 70 : */ 71 : using PipelineStateDescCallback = CShaderTechnique::PipelineStateDescCallback; 72 : CShaderTechniquePtr LoadEffect( 73 : CStrIntern name, const CShaderDefines& defines, const PipelineStateDescCallback& callback); 74 : 75 : /** 76 : * Returns the number of shader effects that are currently loaded. 77 : */ 78 : size_t GetNumEffectsLoaded() const; 79 : 80 : private: 81 0 : struct CacheKey 82 : { 83 : std::string name; 84 : CShaderDefines defines; 85 : 86 0 : bool operator<(const CacheKey& k) const 87 : { 88 0 : if (name < k.name) return true; 89 0 : if (k.name < name) return false; 90 0 : return defines < k.defines; 91 : } 92 : }; 93 : 94 : Renderer::Backend::IDevice* m_Device = nullptr; 95 : 96 : // A CShaderProgram contains expensive backend state, so we ought to cache it. 97 : // The compiled state depends solely on the filename and list of defines, 98 : // so we store that in CacheKey. 99 : // TODO: is this cache useful when we already have an effect cache? 100 : std::map<CacheKey, CShaderProgramPtr> m_ProgramCache; 101 : 102 : /** 103 : * Key for effect cache lookups. 104 : * This stores two separate CShaderDefines because the renderer typically 105 : * has one set from the rendering context and one set from the material; 106 : * by handling both separately here, we avoid the cost of having to merge 107 : * the two sets into a single one before doing the cache lookup. 108 : */ 109 : struct EffectCacheKey 110 : { 111 : CStrIntern name; 112 : CShaderDefines defines; 113 : 114 : bool operator==(const EffectCacheKey& b) const; 115 : }; 116 : 117 : struct EffectCacheKeyHash 118 : { 119 : size_t operator()(const EffectCacheKey& key) const; 120 : }; 121 : 122 : using EffectCacheMap = std::unordered_map<EffectCacheKey, CShaderTechniquePtr, EffectCacheKeyHash>; 123 : EffectCacheMap m_EffectCache; 124 : 125 : // Store the set of shaders that need to be reloaded when the given file is modified 126 : template<typename T> 127 : using HotloadFilesMap = std::unordered_map< 128 : VfsPath, 129 : std::set<std::weak_ptr<T>, std::owner_less<std::weak_ptr<T>>>>; 130 : HotloadFilesMap<CShaderTechnique> m_HotloadTechniques; 131 : HotloadFilesMap<CShaderProgram> m_HotloadPrograms; 132 : 133 : /** 134 : * Load a shader program. 135 : * @param name name of shader XML specification (file is loaded from shaders/${name}.xml) 136 : * @param defines key/value set of preprocessor definitions 137 : * @return loaded program, or null pointer on error 138 : */ 139 : CShaderProgramPtr LoadProgram(const CStr& name, const CShaderDefines& defines); 140 : 141 : bool LoadTechnique(CShaderTechniquePtr& tech); 142 : 143 : static Status ReloadChangedFileCB(void* param, const VfsPath& path); 144 : Status ReloadChangedFile(const VfsPath& path); 145 : 146 : /** 147 : * Associates the file with the technique to be reloaded if the file has changed. 148 : */ 149 : void AddTechniqueFileDependency(const CShaderTechniquePtr& technique, const VfsPath& path); 150 : 151 : /** 152 : * Associates the file with the program to be reloaded if the file has changed. 153 : */ 154 : void AddProgramFileDependency(const CShaderProgramPtr& program, const VfsPath& path); 155 : }; 156 : 157 : #endif // INCLUDED_SHADERMANAGER