Pyrogenesis  trunk
ShaderManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2015 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 #define USE_SHADER_XML_VALIDATION 1
22 
23 #include <boost/unordered_map.hpp>
24 #include <memory>
25 #include <set>
26 
27 #include "graphics/ShaderDefines.h"
28 #include "graphics/ShaderProgram.h"
30 
31 #if USE_SHADER_XML_VALIDATION
32 # include "ps/XML/RelaxNG.h"
33 #endif
34 
35 
36 /**
37  * Shader manager: loads and caches shader programs.
38  *
39  * For a high-level overview of shaders and materials, see
40  * http://trac.wildfiregames.com/wiki/MaterialSystem
41  */
43 {
44 public:
47 
48  /**
49  * Load a shader program.
50  * @param name name of shader XML specification (file is loaded from shaders/${name}.xml)
51  * @param defines key/value set of preprocessor definitions
52  * @return loaded program, or null pointer on error
53  */
54  CShaderProgramPtr LoadProgram(const char* name, const CShaderDefines& defines);
55 
56  /**
57  * Load a shader effect.
58  * Effects can be implemented via many techniques; this returns the best usable technique.
59  * @param name name of effect XML specification (file is loaded from shaders/effects/${name}.xml)
60  * @param defines1,defines2 key/value set of preprocessor definitions; defines2 has higher precedence
61  * @return loaded technique, or empty technique on error
62  */
63  CShaderTechniquePtr LoadEffect(CStrIntern name, const CShaderDefines& defines1, const CShaderDefines& defines2);
64 
65  /**
66  * Load a shader effect, with default system defines (from CRenderer::GetSystemShaderDefines).
67  */
69 
70  /**
71  * Returns the number of shader effects that are currently loaded.
72  */
73  size_t GetNumEffectsLoaded();
74 
75 private:
76 
77  struct CacheKey
78  {
79  std::string name;
81 
82  bool operator<(const CacheKey& k) const
83  {
84  if (name < k.name) return true;
85  if (k.name < name) return false;
86  return defines < k.defines;
87  }
88  };
89 
90  // A CShaderProgram contains expensive GL state, so we ought to cache it.
91  // The compiled state depends solely on the filename and list of defines,
92  // so we store that in CacheKey.
93  // TODO: is this cache useful when we already have an effect cache?
94  std::map<CacheKey, CShaderProgramPtr> m_ProgramCache;
95 
96  /**
97  * Key for effect cache lookups.
98  * This stores two separate CShaderDefines because the renderer typically
99  * has one set from the rendering context and one set from the material;
100  * by handling both separately here, we avoid the cost of having to merge
101  * the two sets into a single one before doing the cache lookup.
102  */
104  {
108 
109  bool operator==(const EffectCacheKey& b) const;
110  };
111 
113  {
114  size_t operator()(const EffectCacheKey& key) const;
115  };
116 
117  typedef boost::unordered_map<EffectCacheKey, CShaderTechniquePtr, EffectCacheKeyHash> EffectCacheMap;
118  EffectCacheMap m_EffectCache;
119 
120  // Store the set of shaders that need to be reloaded when the given file is modified
121  typedef boost::unordered_map<VfsPath, std::set<std::weak_ptr<CShaderProgram>, std::owner_less<std::weak_ptr<CShaderProgram>>>> HotloadFilesMap;
122  HotloadFilesMap m_HotloadFiles;
123 
124  bool NewProgram(const char* name, const CShaderDefines& defines, CShaderProgramPtr& program);
125  bool NewEffect(const char* name, const CShaderDefines& defines, CShaderTechniquePtr& tech);
126 
127  static Status ReloadChangedFileCB(void* param, const VfsPath& path);
128  Status ReloadChangedFile(const VfsPath& path);
129 };
130 
131 #endif // INCLUDED_SHADERMANAGER
CShaderDefines defines2
Definition: ShaderManager.h:107
~CShaderManager()
Definition: ShaderManager.cpp:55
Definition: ShaderManager.h:112
HotloadFilesMap m_HotloadFiles
Definition: ShaderManager.h:122
bool operator<(const CacheKey &k) const
Definition: ShaderManager.h:82
bool operator==(const FCDJointWeightPair &a, const FCDJointWeightPair &b)
Definition: GeomReindex.cpp:59
CShaderDefines defines
Definition: ShaderManager.h:80
std::string name
Definition: ShaderManager.h:79
Key for effect cache lookups.
Definition: ShaderManager.h:103
shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechnique.h:113
std::map< CacheKey, CShaderProgramPtr > m_ProgramCache
Definition: ShaderManager.h:94
bool NewEffect(const char *name, const CShaderDefines &defines, CShaderTechniquePtr &tech)
Definition: ShaderManager.cpp:379
Status ReloadChangedFile(const VfsPath &path)
Definition: ShaderManager.cpp:564
Interned 8-bit strings.
Definition: CStrIntern.h:37
CShaderManager()
Definition: ShaderManager.cpp:40
Definition: path.h:77
CShaderTechniquePtr LoadEffect(CStrIntern name, const CShaderDefines &defines1, const CShaderDefines &defines2)
Load a shader effect.
Definition: ShaderManager.cpp:354
boost::unordered_map< EffectCacheKey, CShaderTechniquePtr, EffectCacheKeyHash > EffectCacheMap
Definition: ShaderManager.h:117
EffectCacheMap m_EffectCache
Definition: ShaderManager.h:118
pthread_key_t key
Definition: wpthread.cpp:140
Definition: ShaderManager.h:77
i64 Status
Error handling system.
Definition: status.h:171
boost::unordered_map< VfsPath, std::set< std::weak_ptr< CShaderProgram >, std::owner_less< std::weak_ptr< CShaderProgram > > > > HotloadFilesMap
Definition: ShaderManager.h:121
static Status ReloadChangedFileCB(void *param, const VfsPath &path)
Definition: ShaderManager.cpp:559
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:133
CShaderDefines defines1
Definition: ShaderManager.h:106
CShaderProgramPtr LoadProgram(const char *name, const CShaderDefines &defines)
Load a shader program.
Definition: ShaderManager.cpp:60
size_t GetNumEffectsLoaded()
Returns the number of shader effects that are currently loaded.
Definition: ShaderManager.cpp:554
bool NewProgram(const char *name, const CShaderDefines &defines, CShaderProgramPtr &program)
Definition: ShaderManager.cpp:105
CStrIntern name
Definition: ShaderManager.h:105
std::shared_ptr< CShaderProgram > CShaderProgramPtr
Definition: ShaderProgramPtr.h:25
Shader manager: loads and caches shader programs.
Definition: ShaderManager.h:42