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