Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLUniformCache.h

raw
1 #pragma once
2
3 #ifndef DOXYGEN_GENERATING_OUTPUT
4 #define NCINE_INCLUDE_OPENGL
5 #include "../../../CommonHeaders.h"
6 #endif
7
8 #include "../../../../Main.h"
9
10 namespace nCine::RHI::GL
11 {
12 class GLUniform;
13
14 /**
15 @brief Caches the value of a single uniform and uploads it to the shader
16
17 Holds a host-side data pointer for one @ref GLUniform and the `Set*` methods that write
18 values into it. A dirty flag tracks whether the cached value differs from what was last
19 uploaded; @ref CommitValue() then flushes it to the GL with the appropriate `glUniform*`
20 call, avoiding redundant uploads. The cache must reference a uniform that is not part of
21 a uniform block.
22 */
23 class GLUniformCache
24 {
25 public:
26 /** @brief Creates an empty cache not associated with any uniform */
27 GLUniformCache();
28 /** @brief Creates a cache for the specified uniform */
29 explicit GLUniformCache(const GLUniform* uniform);
30
31 /** @brief Returns the uniform described by this cache */
32 inline const GLUniform* GetUniform() const {
33 return _uniform;
34 }
35 /** @brief Returns the host-side data pointer holding the cached value */
36 inline const GLubyte* GetDataPointer() const {
37 return _dataPointer;
38 }
39 /** @brief Sets the host-side data pointer holding the cached value */
40 inline void SetDataPointer(GLubyte* dataPointer) {
41 _dataPointer = dataPointer;
42 }
43
44 /** @brief Returns the cached value as a float vector, or `nullptr` if unavailable */
45 const GLfloat* GetFloatVector() const;
46 /** @brief Returns the float component at the specified index */
47 GLfloat GetFloatValue(std::uint32_t index) const;
48 /** @brief Returns the cached value as an integer vector, or `nullptr` if unavailable */
49 const GLint* GetIntVector() const;
50 /** @brief Returns the integer component at the specified index */
51 GLint GetIntValue(std::uint32_t index) const;
52
53 /** @brief Sets the cached value from a float vector and returns whether it succeeded */
54 bool SetFloatVector(const GLfloat* vec);
55 /** @brief Sets a single-component float value and returns whether it succeeded */
56 bool SetFloatValue(GLfloat v0);
57 /** @brief Sets a two-component float value and returns whether it succeeded */
58 bool SetFloatValue(GLfloat v0, GLfloat v1);
59 /** @brief Sets a three-component float value and returns whether it succeeded */
60 bool SetFloatValue(GLfloat v0, GLfloat v1, GLfloat v2);
61 /** @brief Sets a four-component float value and returns whether it succeeded */
62 bool SetFloatValue(GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
63 /** @brief Sets the cached value from an integer vector and returns whether it succeeded */
64 bool SetIntVector(const GLint* vec);
65 /** @brief Sets a single-component integer value and returns whether it succeeded */
66 bool SetIntValue(GLint v0);
67 /** @brief Sets a two-component integer value and returns whether it succeeded */
68 bool SetIntValue(GLint v0, GLint v1);
69 /** @brief Sets a three-component integer value and returns whether it succeeded */
70 bool SetIntValue(GLint v0, GLint v1, GLint v2);
71 /** @brief Sets a four-component integer value and returns whether it succeeded */
72 bool SetIntValue(GLint v0, GLint v1, GLint v2, GLint v3);
73
74 /** @brief Returns whether the cached value still needs to be committed to the GL */
75 inline bool IsDirty() const {
76 return _isDirty;
77 }
78 /** @brief Sets the dirty flag controlling whether the value is committed */
79 inline void SetDirty(bool isDirty) {
80 _isDirty = isDirty;
81 }
82 /**
83 * @brief Uploads the cached value to the shader if it is dirty
84 *
85 * Issues the matching `glUniform*` call for the uniform type and clears the dirty flag.
86 * Does nothing and returns `false` if there is no value, no data pointer or the value is
87 * not dirty.
88 */
89 bool CommitValue();
90
91 private:
92 const GLUniform* _uniform;
93 GLubyte* _dataPointer;
94 // A flag to signal if the uniform needs to be committed
95 bool _isDirty;
96
97 bool CheckFloat() const;
98 bool CheckInt() const;
99 bool CheckComponents(std::uint32_t requiredComponents) const;
100 };
101
102 }