Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ShaderManager.h
Go to the documentation of this file.
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
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
38class CShaderProgram;
39namespace 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 */
48{
49public:
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 */
61
62 /**
63 * Load a shader effect, with empty defines.
64 */
66
67 /**
68 * Load a shader effect with the pipeline state description overwriting.
69 * TODO: we should set all needed states in XML.
70 */
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
80private:
81 struct CacheKey
82 {
83 std::string name;
85
86 bool operator<(const CacheKey& k) const
87 {
88 if (name < k.name) return true;
89 if (k.name < name) return false;
90 return defines < k.defines;
91 }
92 };
93
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 */
110 {
113
114 bool operator==(const EffectCacheKey& b) const;
115 };
116
118 {
119 size_t operator()(const EffectCacheKey& key) const;
120 };
121
122 using EffectCacheMap = std::unordered_map<EffectCacheKey, CShaderTechniquePtr, EffectCacheKeyHash>;
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>>>>;
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
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
Path VfsPath
Definition: HeightMipmap.h:33
std::shared_ptr< CShaderProgram > CShaderProgramPtr
Definition: ShaderProgramPtr.h:28
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:30
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:153
Shader manager: loads and caches shader programs.
Definition: ShaderManager.h:48
bool LoadTechnique(CShaderTechniquePtr &tech)
Definition: ShaderManager.cpp:144
HotloadFilesMap< CShaderProgram > m_HotloadPrograms
Definition: ShaderManager.h:131
Renderer::Backend::IDevice * m_Device
Definition: ShaderManager.h:94
CShaderProgramPtr LoadProgram(const CStr &name, const CShaderDefines &defines)
Load a shader program.
Definition: ShaderManager.cpp:68
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:516
Status ReloadChangedFile(const VfsPath &path)
Definition: ShaderManager.cpp:479
HotloadFilesMap< CShaderTechnique > m_HotloadTechniques
Definition: ShaderManager.h:130
CShaderTechniquePtr LoadEffect(CStrIntern name, const CShaderDefines &defines)
Load a shader effect.
Definition: ShaderManager.cpp:108
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:511
std::unordered_map< VfsPath, std::set< std::weak_ptr< T >, std::owner_less< std::weak_ptr< T > > > > HotloadFilesMap
Definition: ShaderManager.h:129
~CShaderManager()
Definition: ShaderManager.cpp:63
CShaderManager(Renderer::Backend::IDevice *device)
Definition: ShaderManager.cpp:51
std::unordered_map< EffectCacheKey, CShaderTechniquePtr, EffectCacheKeyHash > EffectCacheMap
Definition: ShaderManager.h:122
std::map< CacheKey, CShaderProgramPtr > m_ProgramCache
Definition: ShaderManager.h:100
CShaderTechnique::PipelineStateDescCallback PipelineStateDescCallback
Load a shader effect with the pipeline state description overwriting.
Definition: ShaderManager.h:71
static Status ReloadChangedFileCB(void *param, const VfsPath &path)
Definition: ShaderManager.cpp:474
EffectCacheMap m_EffectCache
Definition: ShaderManager.h:123
size_t GetNumEffectsLoaded() const
Returns the number of shader effects that are currently loaded.
Definition: ShaderManager.cpp:469
A wrapper for backend shader program to handle high-level operations like file reloading and handling...
Definition: ShaderProgram.h:38
std::function< void(Renderer::Backend::SGraphicsPipelineStateDesc &pipelineStateDesc)> PipelineStateDescCallback
Definition: ShaderTechnique.h:64
Interned 8-bit strings.
Definition: CStrIntern.h:42
Definition: path.h:84
Definition: IDevice.h:64
Definition: Canvas2D.h:30
std::int64_t Status
Error handling system.
Definition: status.h:174
Definition: ShaderManager.h:82
CShaderDefines defines
Definition: ShaderManager.h:84
std::string name
Definition: ShaderManager.h:83
bool operator<(const CacheKey &k) const
Definition: ShaderManager.h:86
Definition: ShaderManager.h:118
size_t operator()(const EffectCacheKey &key) const
Definition: ShaderManager.cpp:90
Key for effect cache lookups.
Definition: ShaderManager.h:110
CStrIntern name
Definition: ShaderManager.h:111
CShaderDefines defines
Definition: ShaderManager.h:112
bool operator==(const EffectCacheKey &b) const
Definition: ShaderManager.cpp:98
pthread_key_t key
Definition: wpthread.cpp:141