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