Debian Codegraph @

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

raw
1 #pragma once
2
3 #include "../../../Base/StaticHashMap.h"
4
5 #include "GLUniformBlockCache.h"
6 #include "../RhiFwd.h"
7
8 namespace nCine::RHI::GL
9 {
10 class GLShaderProgram;
11 class GLBufferObject;
12
13 /**
14 @brief Manages the uniform block caches of a shader program
15
16 Owns a @ref GLUniformBlockCache for every active uniform block of a shader program. It
17 provides lookup by name, a shared data pointer setup, a commit operation that copies the
18 block contents into a uniform buffer, and a bind operation that binds the buffer ranges.
19 The set of managed blocks can be restricted to an include-only or exclude list.
20 */
21 class GLShaderUniformBlocks
22 {
23 public:
24 /** @brief Number of buckets in the uniform block cache hashmap */
25 static constexpr std::int32_t UniformBlockCachesHashSize = 4;
26 using UniformHashMapType = StaticHashMap<String, GLUniformBlockCache, UniformBlockCachesHashSize>;
27
28 /** @brief Function that suballocates a range of the given size from the streaming uniform buffer */
29 using UniformRangeAllocator = RHI::BufferRange (*)(std::uint32_t bytes);
30
31 /** @brief Sets the allocator used by @ref CommitUniformBlocks() to place block data into a uniform buffer (registered by the render pipeline at startup) */
32 static void SetUniformRangeAllocator(UniformRangeAllocator allocator);
33
34 /** @brief Creates an instance not associated with any shader program */
35 GLShaderUniformBlocks();
36 /** @brief Creates an instance importing all uniform blocks of the specified shader program */
37 explicit GLShaderUniformBlocks(GLShaderProgram* shaderProgram);
38 /** @brief Creates an instance importing a filtered subset of the uniform blocks of the shader program */
39 GLShaderUniformBlocks(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
40
41 /** @brief Associates a shader program and imports all of its uniform blocks */
42 inline void SetProgram(GLShaderProgram* shaderProgram) {
43 SetProgram(shaderProgram, nullptr, nullptr);
44 }
45 /** @brief Associates a shader program and imports a filtered subset of its uniform blocks */
46 void SetProgram(GLShaderProgram* shaderProgram, const char* includeOnly, const char* exclude);
47 /** @brief Distributes a single host-side data buffer across the managed uniform block caches */
48 void SetUniformsDataPointer(GLubyte* dataPointer);
49
50 /** @brief Returns the number of managed uniform blocks */
51 inline unsigned int GetUniformBlockCount() const {
52 return _uniformBlockCaches.size();
53 }
54 /** @brief Returns whether a uniform block with the specified name is managed */
55 inline bool HasUniformBlock(const char* name) const {
56 return (_uniformBlockCaches.find(String::nullTerminatedView(name)) != nullptr);
57 }
58 /** @brief Returns the uniform block cache with the specified name, or `nullptr` if not found */
59 GLUniformBlockCache* GetUniformBlock(const char* name);
60 /** @brief Returns the hashmap of all managed uniform block caches */
61 inline const UniformHashMapType& GetAllUniformBlocks() const {
62 return _uniformBlockCaches;
63 }
64 /** @brief Copies the managed blocks into a uniform buffer acquired from the buffers manager */
65 void CommitUniformBlocks();
66
67 /** @brief Binds the uniform buffer ranges of the managed blocks for rendering */
68 void Bind();
69
70 private:
71 static UniformRangeAllocator _uniformRangeAllocator;
72
73 GLShaderProgram* _shaderProgram;
74 // Pointer to the data of the first uniform block
75 GLubyte* _dataPointer;
76
77 // Uniform buffer range for binding
78 RHI::BufferRange _uboParams;
79
80 UniformHashMapType _uniformBlockCaches;
81
82 // Imports the uniform blocks with the option of including only some or excluding others
83 void ImportUniformBlocks(const char* includeOnly, const char* exclude);
84 };
85 }