Pyrogenesis  trunk
ShaderDefines.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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_SHADERDEFINES
19 #define INCLUDED_SHADERDEFINES
20 
22 #include "ps/CStr.h"
23 #include "ps/CStrIntern.h"
24 
25 #include <boost/unordered_map.hpp>
26 
27 class CVector4D;
28 
29 /**
30  * Represents a mapping of name strings to value, for use with
31  * CShaderDefines (values are strings) and CShaderUniforms (values are vec4s).
32  *
33  * Stored as interned vectors of name-value pairs, to support high performance
34  * comparison operators.
35  *
36  * Not thread-safe - must only be used from the main thread.
37  */
38 template<typename value_t>
40 {
41 public:
42  /**
43  * Create an empty map of defines.
44  */
45  CShaderParams();
46 
47  /**
48  * Add a name and associated value to the map of parameters.
49  * If the name is already defined, its value will be replaced.
50  */
51  void Set(CStrIntern name, const value_t& value);
52 
53  /**
54  * Add all the names and values from another set of parameters.
55  * If any name is already defined in this object, its value will be replaced.
56  */
57  void SetMany(const CShaderParams& params);
58 
59  /**
60  * Return a copy of the current name/value mapping.
61  */
62  std::map<CStrIntern, value_t> GetMap() const;
63 
64  /**
65  * Return a hash of the current mapping.
66  */
67  size_t GetHash() const;
68 
69  /**
70  * Compare with some arbitrary total order.
71  * The order may be different each time the application is run
72  * (it is based on interned memory addresses).
73  */
74  bool operator<(const CShaderParams& b) const
75  {
76  return m_Items < b.m_Items;
77  }
78 
79  /**
80  * Fast equality comparison.
81  */
82  bool operator==(const CShaderParams& b) const
83  {
84  return m_Items == b.m_Items;
85  }
86 
87  /**
88  * Fast inequality comparison.
89  */
90  bool operator!=(const CShaderParams& b) const
91  {
92  return m_Items != b.m_Items;
93  }
94 
95  struct SItems
96  {
97  // Name/value pair
98  typedef std::pair<CStrIntern, value_t> Item;
99 
100  // Sorted by name; no duplicated names
101  std::vector<Item> items;
102 
103  size_t hash;
104 
105  void RecalcHash();
106  };
107 
108 protected:
109  SItems* m_Items; // interned value
110 
111 private:
112  typedef boost::unordered_map<SItems, shared_ptr<SItems> > InternedItems_t;
113  static InternedItems_t s_InternedItems;
114 
115  /**
116  * Returns a pointer to an SItems equal to @p items.
117  * The pointer will be valid forever, and the same pointer will be returned
118  * for any subsequent requests for an equal items list.
119  */
120  static SItems* GetInterned(const SItems& items);
121 
122  CShaderParams(SItems* items);
123  static CShaderParams CreateEmpty();
125 };
126 
127 /**
128  * Represents a mapping of name strings to value strings, for use with
129  * \#if and \#ifdef and similar conditionals in shaders.
130  *
131  * Not thread-safe - must only be used from the main thread.
132  */
133 class CShaderDefines : public CShaderParams<CStrIntern>
134 {
135 public:
136  /**
137  * Add a name and associated value to the map of defines.
138  * If the name is already defined, its value will be replaced.
139  */
140  void Add(CStrIntern name, CStrIntern value);
141 
142  /**
143  * Return the value for the given name as an integer, or 0 if not defined.
144  */
145  int GetInt(const char* name) const;
146 };
147 
148 /**
149  * Represents a mapping of name strings to value CVector4Ds, for use with
150  * uniforms in shaders.
151  *
152  * Not thread-safe - must only be used from the main thread.
153  */
154 class CShaderUniforms : public CShaderParams<CVector4D>
155 {
156 public:
157  /**
158  * Add a name and associated value to the map of uniforms.
159  * If the name is already defined, its value will be replaced.
160  */
161  void Add(const char* name, const CVector4D& value);
162 
163  /**
164  * Return the value for the given name, or (0,0,0,0) if not defined.
165  */
166  CVector4D GetVector(const char* name) const;
167 
168  /**
169  * Bind the collection of uniforms onto the given shader.
170  */
171  void BindUniforms(const CShaderProgramPtr& shader) const;
172 };
173 
174 // Add here the types of queries we can make in the renderer
176 {
180 };
181 
182 /**
183  * Uniform values that need to be evaluated in the renderer.
184  *
185  * Not thread-safe - must only be used from the main thread.
186  */
188 {
189 public:
190  typedef std::pair<int, CStrIntern> RenderQuery;
191 
192  void Add(const char* name);
193  size_t GetSize() const { return m_Items.size(); }
194  RenderQuery GetItem(size_t i) const { return m_Items[i]; }
195 private:
196  std::vector<RenderQuery> m_Items;
197 };
198 
199 
201 {
203 };
204 
206 {
207 public:
208  struct CondDefine
209  {
213  std::vector<float> m_CondArgs;
214  };
215 
216  void Add(const char* defname, const char* defvalue, int type, std::vector<float> &args);
217  size_t GetSize() const { return m_Defines.size(); }
218  const CondDefine& GetItem(size_t i) const { return m_Defines[i]; }
219 
220 private:
221  std::vector<CondDefine> m_Defines;
222 };
223 
224 #endif // INCLUDED_SHADERDEFINES
std::pair< CStrIntern, value_t > Item
Definition: ShaderDefines.h:98
int m_CondType
Definition: ShaderDefines.h:212
Definition: ShaderDefines.h:177
DEFINE_CONDITION_TYPES
Definition: ShaderDefines.h:200
Definition: ShaderDefines.h:202
bool operator<(const CShaderParams &b) const
Compare with some arbitrary total order.
Definition: ShaderDefines.h:74
Definition: ShaderDefines.h:179
Uniform values that need to be evaluated in the renderer.
Definition: ShaderDefines.h:187
std::map< CStrIntern, value_t > GetMap() const
Return a copy of the current name/value mapping.
Definition: ShaderDefines.cpp:163
size_t hash
Definition: ShaderDefines.h:103
static CShaderParams s_Empty
Definition: ShaderDefines.h:124
Definition: Vector4D.h:28
RenderQuery GetItem(size_t i) const
Definition: ShaderDefines.h:194
Definition: ShaderDefines.h:95
std::vector< float > m_CondArgs
Definition: ShaderDefines.h:213
boost::unordered_map< SItems, shared_ptr< SItems > > InternedItems_t
Definition: ShaderDefines.h:112
bool operator!=(const CShaderParams &b) const
Fast inequality comparison.
Definition: ShaderDefines.h:90
Interned 8-bit strings.
Definition: CStrIntern.h:37
CShaderParams()
Create an empty map of defines.
Definition: ShaderDefines.cpp:102
size_t GetSize() const
Definition: ShaderDefines.h:217
std::vector< CondDefine > m_Defines
Definition: ShaderDefines.h:221
Definition: ShaderDefines.h:178
Definition: ShaderDefines.h:205
CStrIntern m_DefName
Definition: ShaderDefines.h:210
static CShaderParams CreateEmpty()
Definition: ShaderDefines.cpp:113
CStrIntern m_DefValue
Definition: ShaderDefines.h:211
Definition: ShaderDefines.h:208
RENDER_QUERIES
Definition: ShaderDefines.h:175
SItems * m_Items
Definition: ShaderDefines.h:109
size_t GetHash() const
Return a hash of the current mapping.
Definition: ShaderDefines.cpp:172
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:133
Represents a mapping of name strings to value, for use with CShaderDefines (values are strings) and C...
Definition: ShaderDefines.h:39
Represents a mapping of name strings to value CVector4Ds, for use with uniforms in shaders...
Definition: ShaderDefines.h:154
void SetMany(const CShaderParams &params)
Add all the names and values from another set of parameters.
Definition: ShaderDefines.cpp:147
bool operator==(const CShaderParams &b) const
Fast equality comparison.
Definition: ShaderDefines.h:82
void RecalcHash()
Definition: ShaderDefines.cpp:178
static SItems * GetInterned(const SItems &items)
Returns a pointer to an SItems equal to items.
Definition: ShaderDefines.cpp:83
const CondDefine & GetItem(size_t i) const
Definition: ShaderDefines.h:218
std::vector< Item > items
Definition: ShaderDefines.h:101
void Set(CStrIntern name, const value_t &value)
Add a name and associated value to the map of parameters.
Definition: ShaderDefines.cpp:121
size_t GetSize() const
Definition: ShaderDefines.h:193
static InternedItems_t s_InternedItems
Definition: ShaderDefines.h:113
std::vector< RenderQuery > m_Items
Definition: ShaderDefines.h:196
std::shared_ptr< CShaderProgram > CShaderProgramPtr
Definition: ShaderProgramPtr.h:25
std::pair< int, CStrIntern > RenderQuery
Definition: ShaderDefines.h:190