Pyrogenesis trunk
ShaderManager.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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
26
27#include <functional>
28#include <memory>
29#include <set>
30#include <unordered_map>
31
32/**
33 * Shader manager: loads and caches shader programs.
34 *
35 * For a high-level overview of shaders and materials, see
36 * http://trac.wildfiregames.com/wiki/MaterialSystem
37 */
39{
40public:
43
44 /**
45 * Load a shader effect.
46 * Effects can be implemented via many techniques; this returns the best usable technique.
47 * @param name name of effect XML specification (file is loaded from shaders/effects/${name}.xml)
48 * @param defines key/value set of preprocessor definitions
49 * @return loaded technique, or empty technique on error
50 */
52
53 /**
54 * Load a shader effect, with empty defines.
55 */
57
58 /**
59 * Load a shader effect with the pipeline state description overwriting.
60 * TODO: we should set all needed states in XML.
61 */
64 CStrIntern name, const CShaderDefines& defines, const PipelineStateDescCallback& callback);
65
66 /**
67 * Returns the number of shader effects that are currently loaded.
68 */
69 size_t GetNumEffectsLoaded() const;
70
71private:
72 struct CacheKey
73 {
74 std::string name;
76
77 bool operator<(const CacheKey& k) const
78 {
79 if (name < k.name) return true;
80 if (k.name < name) return false;
81 return defines < k.defines;
82 }
83 };
84
86
87 // A CShaderProgram contains expensive backend state, so we ought to cache it.
88 // The compiled state depends solely on the filename and list of defines,
89 // so we store that in CacheKey.
90 // TODO: is this cache useful when we already have an effect cache?
91 std::map<CacheKey, CShaderProgramPtr> m_ProgramCache;
92
93 /**
94 * Key for effect cache lookups.
95 * This stores two separate CShaderDefines because the renderer typically
96 * has one set from the rendering context and one set from the material;
97 * by handling both separately here, we avoid the cost of having to merge
98 * the two sets into a single one before doing the cache lookup.
99 */
101 {
104
105 bool operator==(const EffectCacheKey& b) const;
106 };
107
109 {
110 size_t operator()(const EffectCacheKey& key) const;
111 };
112
113 using EffectCacheMap = std::unordered_map<EffectCacheKey, CShaderTechniquePtr, EffectCacheKeyHash>;
115
116 // Store the set of shaders that need to be reloaded when the given file is modified
117 template<typename T>
118 using HotloadFilesMap = std::unordered_map<
119 VfsPath,
120 std::set<std::weak_ptr<T>, std::owner_less<std::weak_ptr<T>>>>;
123
124 /**
125 * Load a shader program.
126 * @param name name of shader XML specification (file is loaded from shaders/${name}.xml)
127 * @param defines key/value set of preprocessor definitions
128 * @return loaded program, or null pointer on error
129 */
130 CShaderProgramPtr LoadProgram(const CStr& name, const CShaderDefines& defines);
131
133
134 static Status ReloadChangedFileCB(void* param, const VfsPath& path);
135 Status ReloadChangedFile(const VfsPath& path);
136
137 /**
138 * Associates the file with the technique to be reloaded if the file has changed.
139 */
140 void AddTechniqueFileDependency(const CShaderTechniquePtr& technique, const VfsPath& path);
141
142 /**
143 * Associates the file with the program to be reloaded if the file has changed.
144 */
145 void AddProgramFileDependency(const CShaderProgramPtr& program, const VfsPath& path);
146};
147
148#endif // INCLUDED_SHADERMANAGER
Path VfsPath
Definition: HeightMipmap.h:30
std::shared_ptr< CShaderProgram > CShaderProgramPtr
Definition: ShaderProgramPtr.h:26
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:28
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:147
Shader manager: loads and caches shader programs.
Definition: ShaderManager.h:39
bool LoadTechnique(CShaderTechniquePtr &tech)
Definition: ShaderManager.cpp:145
HotloadFilesMap< CShaderProgram > m_HotloadPrograms
Definition: ShaderManager.h:122
Renderer::Backend::IDevice * m_Device
Definition: ShaderManager.h:85
CShaderProgramPtr LoadProgram(const CStr &name, const CShaderDefines &defines)
Load a shader program.
Definition: ShaderManager.cpp:69
void AddProgramFileDependency(const CShaderProgramPtr &program, const VfsPath &path)
Associates the file with the program to be reloaded if the file has changed.
Definition: ShaderManager.cpp:519
Status ReloadChangedFile(const VfsPath &path)
Definition: ShaderManager.cpp:485
HotloadFilesMap< CShaderTechnique > m_HotloadTechniques
Definition: ShaderManager.h:121
CShaderTechniquePtr LoadEffect(CStrIntern name, const CShaderDefines &defines)
Load a shader effect.
Definition: ShaderManager.cpp:109
void AddTechniqueFileDependency(const CShaderTechniquePtr &technique, const VfsPath &path)
Associates the file with the technique to be reloaded if the file has changed.
Definition: ShaderManager.cpp:514
std::unordered_map< VfsPath, std::set< std::weak_ptr< T >, std::owner_less< std::weak_ptr< T > > > > HotloadFilesMap
Definition: ShaderManager.h:120
~CShaderManager()
Definition: ShaderManager.cpp:64
CShaderManager(Renderer::Backend::IDevice *device)
Definition: ShaderManager.cpp:48
std::unordered_map< EffectCacheKey, CShaderTechniquePtr, EffectCacheKeyHash > EffectCacheMap
Definition: ShaderManager.h:113
std::map< CacheKey, CShaderProgramPtr > m_ProgramCache
Definition: ShaderManager.h:91
CShaderTechnique::PipelineStateDescCallback PipelineStateDescCallback
Load a shader effect with the pipeline state description overwriting.
Definition: ShaderManager.h:62
static Status ReloadChangedFileCB(void *param, const VfsPath &path)
Definition: ShaderManager.cpp:480
EffectCacheMap m_EffectCache
Definition: ShaderManager.h:114
size_t GetNumEffectsLoaded() const
Returns the number of shader effects that are currently loaded.
Definition: ShaderManager.cpp:475
std::function< void(Renderer::Backend::SGraphicsPipelineStateDesc &pipelineStateDesc)> PipelineStateDescCallback
Definition: ShaderTechnique.h:63
Interned 8-bit strings.
Definition: CStrIntern.h:38
Definition: path.h:80
Definition: IDevice.h:48
i64 Status
Error handling system.
Definition: status.h:173
Definition: ShaderManager.h:73
CShaderDefines defines
Definition: ShaderManager.h:75
std::string name
Definition: ShaderManager.h:74
bool operator<(const CacheKey &k) const
Definition: ShaderManager.h:77
Definition: ShaderManager.h:109
size_t operator()(const EffectCacheKey &key) const
Definition: ShaderManager.cpp:91
Key for effect cache lookups.
Definition: ShaderManager.h:101
CStrIntern name
Definition: ShaderManager.h:102
CShaderDefines defines
Definition: ShaderManager.h:103
bool operator==(const EffectCacheKey &b) const
Definition: ShaderManager.cpp:99
pthread_key_t key
Definition: wpthread.cpp:149