Skip to content

Commit ef2400e

Browse files
committed
refactor OpenGL UboManager, UboBlocks, and Legacy cleanup
1 parent 7f59bb9 commit ef2400e

5 files changed

Lines changed: 62 additions & 49 deletions

File tree

src/opengl/UboBlocks.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ XFOREACH_SHARED_VBO(X_TYPE)
9898
XFOREACH_SHARED_VBO(X_ASSERT)
9999
#undef X_ASSERT
100100

101+
template<SharedVboEnum T>
102+
using BlockType_t = typename BlockType<T>::type;
103+
101104
template<std::size_t... Is>
102105
auto MakeSharedVboBlocksHelper(std::index_sequence<Is...>)
103-
-> std::tuple<typename BlockType<static_cast<SharedVboEnum>(Is)>::type...>;
106+
-> std::tuple<BlockType_t<static_cast<SharedVboEnum>(Is)>...>;
104107

105108
using SharedVboBlocks = decltype(MakeSharedVboBlocksHelper(
106109
std::make_index_sequence<NUM_SHARED_VBOS>{}));

src/opengl/UboManager.h

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,46 @@ namespace Legacy {
2929
*
3030
* Note: This class is not thread-safe.
3131
*/
32-
class UboManager final
32+
class NODISCARD UboManager final
3333
{
3434
public:
35-
using RebuildFunction = std::function<void(Legacy::Functions &gl)>;
35+
using RebuildFunction = std::function<void(Functions &gl)>;
3636

37+
private:
38+
EnumIndexedArray<RebuildFunction, SharedVboEnum> m_rebuildFunctions;
39+
EnumIndexedArray<std::optional<GLuint>, SharedVboEnum> m_boundBuffers;
40+
41+
// Tuple of all block types for shadow storage.
42+
SharedVboBlocks m_shadowBlocks;
43+
44+
public:
3745
UboManager() { invalidateAll(); }
46+
~UboManager() = default;
3847
DELETE_CTORS_AND_ASSIGN_OPS(UboManager);
3948

4049
public:
4150
/**
4251
* @brief Accesses the CPU-side shadow copy of a UBO block by its enum.
4352
*/
44-
template<Legacy::SharedVboEnum Block>
45-
typename Legacy::BlockType<Block>::type &get()
53+
template<SharedVboEnum Block>
54+
NODISCARD BlockType_t<Block> &get()
4655
{
47-
return std::get<typename Legacy::BlockType<Block>::type>(m_shadowBlocks);
56+
return std::get<BlockType_t<Block>>(m_shadowBlocks);
4857
}
4958

5059
/**
5160
* @brief Accesses the CPU-side shadow copy of a UBO block by its enum (const).
5261
*/
53-
template<Legacy::SharedVboEnum Block>
54-
const typename Legacy::BlockType<Block>::type &get() const
62+
template<SharedVboEnum Block>
63+
NODISCARD const BlockType_t<Block> &get() const
5564
{
56-
return std::get<typename Legacy::BlockType<Block>::type>(m_shadowBlocks);
65+
return std::get<BlockType_t<Block>>(m_shadowBlocks);
5766
}
5867

5968
/**
6069
* @brief Marks a UBO block as dirty by resetting its bound state.
6170
*/
62-
void invalidate(Legacy::SharedVboEnum block) { m_boundBuffers[block] = std::nullopt; }
71+
void invalidate(const SharedVboEnum block) { m_boundBuffers[block] = std::nullopt; }
6372

6473
/**
6574
* @brief Marks all UBO blocks as dirty.
@@ -78,7 +87,7 @@ class UboManager final
7887
* will trigger a debug assertion. Set to true only when an
7988
* overwrite is intentional.
8089
*/
81-
void registerRebuildFunction(Legacy::SharedVboEnum block,
90+
void registerRebuildFunction(const SharedVboEnum block,
8291
RebuildFunction func,
8392
bool allowOverwrite = false)
8493
{
@@ -93,29 +102,32 @@ class UboManager final
93102
/**
94103
* @brief Unregisters a rebuild function for a UBO block.
95104
*/
96-
void unregisterRebuildFunction(Legacy::SharedVboEnum block)
105+
void unregisterRebuildFunction(const SharedVboEnum block)
97106
{
98107
m_rebuildFunctions[block] = nullptr;
99108
}
100109

101110
/**
102111
* @brief Checks if a UBO block is currently dirty/invalid.
103112
*/
104-
bool isInvalid(Legacy::SharedVboEnum block) const { return !m_boundBuffers[block].has_value(); }
113+
NODISCARD bool isInvalid(const SharedVboEnum block) const
114+
{
115+
return !m_boundBuffers[block].has_value();
116+
}
105117

106118
/**
107119
* @brief Rebuilds the UBO if it's invalid using the registered rebuild function.
108120
* @return The bound buffer ID, or 0 if failed.
109121
*/
110-
GLuint updateIfInvalid(Legacy::Functions &gl, Legacy::SharedVboEnum block)
122+
ALLOW_DISCARD GLuint updateIfInvalid(Functions &gl, const SharedVboEnum block)
111123
{
112124
if (const auto bound = m_boundBuffers[block]) {
113125
return *bound;
114126
}
115127

116128
const auto &func = m_rebuildFunctions[block];
117129
if (!func) {
118-
const char *name = Legacy::Functions::getUniformBlockName(block);
130+
const char *name = Functions::getUniformBlockName(block);
119131
MMLOG_ERROR() << "UboManager::updateIfInvalid: UBO block '" << name
120132
<< "' is invalid and no rebuild function is registered";
121133
throw std::runtime_error("UBO block '" + std::string(name)
@@ -128,7 +140,7 @@ class UboManager final
128140
return *bound;
129141
}
130142

131-
const char *name = Legacy::Functions::getUniformBlockName(block);
143+
const char *name = Functions::getUniformBlockName(block);
132144
MMLOG_ERROR() << "UboManager::updateIfInvalid: rebuild function failed to call "
133145
"update() for block '"
134146
<< name << "'";
@@ -144,11 +156,12 @@ class UboManager final
144156
* Overload for bulk vector data.
145157
*/
146158
template<typename T, typename A>
147-
GLuint update(Legacy::Functions &gl, Legacy::SharedVboEnum block, const std::vector<T, A> &data)
159+
ALLOW_DISCARD GLuint update(Functions &gl,
160+
const SharedVboEnum block,
161+
const std::vector<T, A> &data)
148162
{
149-
Legacy::VBO &vbo = getOrCreateVbo(gl, block);
150-
static_cast<void>(
151-
gl.setVbo(GL_UNIFORM_BUFFER, vbo.get(), data, BufferUsageEnum::DYNAMIC_DRAW));
163+
VBO &vbo = getOrCreateVbo(gl, block);
164+
gl.setVbo(GL_UNIFORM_BUFFER, vbo.get(), data, BufferUsageEnum::DYNAMIC_DRAW);
152165
return bind_internal(gl, block, vbo.get());
153166
}
154167

@@ -160,9 +173,9 @@ class UboManager final
160173
* Overload for single trivially-copyable objects.
161174
*/
162175
template<typename T>
163-
GLuint update(Legacy::Functions &gl, Legacy::SharedVboEnum block, const T &data)
176+
ALLOW_DISCARD GLuint update(Functions &gl, const SharedVboEnum block, const T &data)
164177
{
165-
Legacy::VBO &vbo = getOrCreateVbo(gl, block);
178+
VBO &vbo = getOrCreateVbo(gl, block);
166179
gl.setVbo(GL_UNIFORM_BUFFER, vbo.get(), data, BufferUsageEnum::DYNAMIC_DRAW);
167180
return bind_internal(gl, block, vbo.get());
168181
}
@@ -172,8 +185,8 @@ class UboManager final
172185
* Enforces the correct data structure for the given block identifier.
173186
* Also updates the shadow copy.
174187
*/
175-
template<Legacy::SharedVboEnum Block>
176-
GLuint update(Legacy::Functions &gl, const typename Legacy::BlockType<Block>::type &data)
188+
template<SharedVboEnum Block>
189+
ALLOW_DISCARD GLuint update(Functions &gl, const BlockType_t<Block> &data)
177190
{
178191
get<Block>() = data;
179192
return update(gl, Block, data);
@@ -182,8 +195,8 @@ class UboManager final
182195
/**
183196
* @brief Syncs the entire shadow copy of a block to the GPU.
184197
*/
185-
template<Legacy::SharedVboEnum Block>
186-
GLuint sync(Legacy::Functions &gl)
198+
template<SharedVboEnum Block>
199+
ALLOW_DISCARD GLuint sync(Functions &gl)
187200
{
188201
return update(gl, Block, get<Block>());
189202
}
@@ -193,10 +206,10 @@ class UboManager final
193206
* @param gl Legacy functions.
194207
* @param members Pointers to the members in the block struct.
195208
*/
196-
template<Legacy::SharedVboEnum Block, typename T, typename... Us>
197-
void syncFields(Legacy::Functions &gl, Us T::*...members)
209+
template<SharedVboEnum Block, typename T, typename... Us>
210+
void syncFields(Functions &gl, Us T::*...members)
198211
{
199-
using BlockType = typename Legacy::BlockType<Block>::type;
212+
using BlockType = BlockType_t<Block>;
200213
static_assert(std::is_same_v<T, BlockType>, "Members must belong to the correct block type");
201214
static_assert(std::is_standard_layout_v<BlockType>,
202215
"Block type must have standard layout for offset calculation");
@@ -209,7 +222,7 @@ class UboManager final
209222
}
210223

211224
const auto &blockData = get<Block>();
212-
Legacy::VBO &vbo = getOrCreateVbo(gl, Block);
225+
VBO &vbo = getOrCreateVbo(gl, Block);
213226
gl.glBindBuffer(GL_UNIFORM_BUFFER, vbo.get());
214227

215228
(gl.glBufferSubData(GL_UNIFORM_BUFFER,
@@ -223,16 +236,16 @@ class UboManager final
223236
gl.glBindBuffer(GL_UNIFORM_BUFFER, 0);
224237

225238
// Ensure it's bound to the correct point.
226-
bind_internal(gl, Block, vbo.get());
239+
std::ignore = bind_internal(gl, Block, vbo.get());
227240
}
228241

229242
/**
230243
* @brief Syncs a specific field of a block to the GPU.
231244
* @param gl Legacy functions.
232245
* @param member Pointer to the member in the block struct.
233246
*/
234-
template<Legacy::SharedVboEnum Block, typename T, typename U>
235-
void syncField(Legacy::Functions &gl, U T::*member)
247+
template<SharedVboEnum Block, typename T, typename U>
248+
void syncField(Functions &gl, U T::*member)
236249
{
237250
syncFields<Block>(gl, member);
238251
}
@@ -241,7 +254,7 @@ class UboManager final
241254
* @brief Binds the UBO to its assigned point.
242255
* If invalid and a rebuild function is registered, it will be updated first.
243256
*/
244-
void bind(Legacy::Functions &gl, Legacy::SharedVboEnum block)
257+
void bind(Functions &gl, const SharedVboEnum block)
245258
{
246259
const GLuint buffer = updateIfInvalid(gl, block);
247260

@@ -259,10 +272,10 @@ class UboManager final
259272
}
260273

261274
private:
262-
Legacy::VBO &getOrCreateVbo(Legacy::Functions &gl, Legacy::SharedVboEnum block)
275+
NODISCARD VBO &getOrCreateVbo(Functions &gl, const SharedVboEnum block)
263276
{
264277
const auto sharedVbo = gl.getSharedVbos().get(block);
265-
Legacy::VBO &vbo = deref(sharedVbo);
278+
VBO &vbo = deref(sharedVbo);
266279

267280
if (!vbo) {
268281
vbo.emplace(gl.shared_from_this());
@@ -275,13 +288,13 @@ class UboManager final
275288
* @brief Binds the UBO to its assigned point.
276289
* @return The bound buffer ID.
277290
*
278-
* Note: This implementation explicitly assumes that Legacy::SharedVboEnum values
291+
* Note: This implementation explicitly assumes that SharedVboEnum values
279292
* are 0-based, contiguous, and directly correspond to UBO binding indices in
280293
* shader blocks.
281294
*/
282-
GLuint bind_internal(Legacy::Functions &gl, Legacy::SharedVboEnum block, GLuint buffer)
295+
NODISCARD GLuint bind_internal(Functions &gl, const SharedVboEnum block, const GLuint buffer)
283296
{
284-
const auto bindingIndex = Legacy::getUboBindingIndex(block);
297+
const auto bindingIndex = getUboBindingIndex(block);
285298
assert(static_cast<std::size_t>(bindingIndex) < m_boundBuffers.size());
286299

287300
auto &bound = m_boundBuffers[block];
@@ -291,13 +304,6 @@ class UboManager final
291304
}
292305
return buffer;
293306
}
294-
295-
private:
296-
EnumIndexedArray<RebuildFunction, Legacy::SharedVboEnum> m_rebuildFunctions;
297-
EnumIndexedArray<std::optional<GLuint>, Legacy::SharedVboEnum> m_boundBuffers;
298-
299-
// Tuple of all block types for shadow storage.
300-
Legacy::SharedVboBlocks m_shadowBlocks;
301307
};
302308

303309
} // namespace Legacy

src/opengl/Weather.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class NODISCARD GLWeather final
110110
NODISCARD T applyTransition(float startTime, T startVal, T targetVal) const;
111111

112112
template<typename T>
113-
struct TransitionPair
113+
struct NODISCARD TransitionPair final
114114
{
115115
T &start;
116116
T target;

src/opengl/legacy/Legacy.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ class NODISCARD Functions : protected QOpenGLExtraFunctions,
330330
/// platform-specific (ES vs GL)
331331
NODISCARD const char *getShaderVersion() const { return virt_getShaderVersion(); }
332332

333-
protected:
333+
private:
334334
NODISCARD virtual bool virt_canRenderQuads() = 0;
335335
NODISCARD virtual std::optional<GLenum> virt_toGLenum(DrawModeEnum mode) = 0;
336336
virtual void virt_enableProgramPointSize(bool enable) = 0;
@@ -344,7 +344,10 @@ class NODISCARD Functions : protected QOpenGLExtraFunctions,
344344
NODISCARD bool canRenderQuads() { return virt_canRenderQuads(); }
345345

346346
/// platform-specific (ES vs GL)
347-
NODISCARD std::optional<GLenum> toGLenum(DrawModeEnum mode) { return virt_toGLenum(mode); }
347+
NODISCARD std::optional<GLenum> toGLenum(const DrawModeEnum mode)
348+
{
349+
return virt_toGLenum(mode);
350+
}
348351

349352
protected:
350353
private:

src/opengl/legacy/TFO.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Legacy {
1010

11+
// TFO = Transform Feedback Object
1112
class NODISCARD TFO final
1213
{
1314
private:

0 commit comments

Comments
 (0)