Debian Codegraph @

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

raw
1 #pragma once
2
3 #include "GLUniformCache.h"
4 #include "../../../Base/StaticHashMap.h"
5
6 #include <string>
7
8 namespace nCine::RHI::GL
9 {
10 class GLShaderProgram;
11
12 /**
13 @brief Manages the uniform caches of a shader program
14
15 Owns a @ref GLUniformCache for every active uniform of a shader program that is not part
16 of a uniform block. It provides lookup by name, a shared data pointer setup, and a commit
17 operation that uploads all dirty uniforms to the GL. The set of managed uniforms can be
18 restricted to an include-only or exclude list.
19 */
20 class GLShaderUniforms
21 {
22 public:
23 /** @brief Number of buckets in the uniform cache hashmap */
24 static constexpr std::uint32_t UniformCachesHashSize = 16;
25 // TODO: Using FNV1aHashFunc because other hash functions are causing graphics glitches for some reason
26 using UniformHashMapType = StaticHashMap<String, GLUniformCache, UniformCachesHashSize, FNV1aHashFunc<String>>;
27
28 /** @brief Creates an instance not associated with any shader program */
29 GLShaderUniforms();
30 /** @brief Creates an instance importing all uniforms of the specified shader program */
31 explicit GLShaderUniforms(GLShaderProgram* shaderProgram);
32 /** @brief Creates an instance importing a filtered subset of the uniforms of the shader program */
33 GLShaderUniforms(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
34
35 /** @brief Associates a shader program and imports all of its uniforms */
36 inline void SetProgram(GLShaderProgram* shaderProgram) {
37 SetProgram(shaderProgram, nullptr, nullptr);
38 }
39 /** @brief Associates a shader program and imports a filtered subset of its uniforms */
40 void SetProgram(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
41 /** @brief Distributes a single host-side data buffer across the managed uniform caches */
42 void SetUniformsDataPointer(GLubyte* dataPointer);
43 /** @brief Sets the dirty flag of all managed uniform caches */
44 void SetDirty(bool isDirty);
45
46 /** @brief Returns the number of managed uniforms */
47 inline std::uint32_t GetUniformCount() const {
48 return _uniformCaches.size();
49 }
50 /** @brief Returns whether a uniform with the specified name is managed */
51 inline bool HasUniform(const char* name) const {
52 return (_uniformCaches.find(String::nullTerminatedView(name)) != nullptr);
53 }
54 /** @brief Returns the uniform cache with the specified name, or `nullptr` if not found */
55 GLUniformCache* GetUniform(const char* name);
56 /** @brief Returns the hashmap of all managed uniform caches */
57 inline const UniformHashMapType& GetAllUniforms() const {
58 return _uniformCaches;
59 }
60 /**
61 * @brief Notes that a managed uniform cache may have been modified externally
62 *
63 * Called automatically by the non-const @ref GetUniform(), it only has to be called
64 * explicitly when a uniform is written through a long-lived cached pointer.
65 */
66 inline void MarkDirty() {
67 _maybeDirty = true;
68 }
69 /** @brief Uses the program and uploads all dirty uniforms to the GL */
70 void CommitUniforms();
71
72 private:
73 GLShaderProgram* _shaderProgram;
74 // Conservative flag for the commit early-out, set whenever a uniform cache may have been dirtied
75 bool _maybeDirty;
76 UniformHashMapType _uniformCaches;
77
78 // Imports the uniforms with the option of including only some or excluding others
79 void ImportUniforms(const char* includeOnly, const char* exclude);
80 };
81 }