Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLUniformBlockCache.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 "GLUniformCache.h"
9 #include "../../../Base/StaticHashMap.h"
10
11 namespace nCine::RHI::GL
12 {
13 class GLUniformBlock;
14 class GLBufferObject;
15
16 /**
17 @brief Caches the contents of a uniform block and uploads it through a buffer object
18
19 Holds a host-side data buffer mirroring a @ref GLUniformBlock and manages a
20 @ref GLUniformCache for each of the block's member uniforms, pointing each one into the
21 appropriate offset of the shared buffer. The contents are uploaded to the GL as a single
22 uniform buffer object (UBO).
23 */
24 class GLUniformBlockCache
25 {
26 public:
27 /** @brief Creates an empty cache not associated with any uniform block */
28 GLUniformBlockCache();
29 /** @brief Creates a cache for the specified uniform block */
30 explicit GLUniformBlockCache(GLUniformBlock* uniformBlock);
31
32 /** @brief Returns the uniform block described by this cache */
33 inline const GLUniformBlock* uniformBlock() const {
34 return _uniformBlock;
35 }
36 /** @brief Wrapper around @ref GLUniformBlock::GetIndex() */
37 GLuint GetIndex() const;
38 /** @brief Wrapper around @ref GLUniformBlock::GetBindingIndex() */
39 GLuint GetBindingIndex() const;
40 /** @brief Wrapper around @ref GLUniformBlock::GetSize() */
41 GLint GetSize() const;
42 /** @brief Wrapper around @ref GLUniformBlock::GetAlignAmount() */
43 std::uint8_t GetAlignAmount() const;
44
45 /** @brief Returns the host-side data buffer mirroring the block */
46 inline GLubyte* GetDataPointer() {
47 return _dataPointer;
48 }
49 /** @brief Returns the host-side data buffer mirroring the block */
50 inline const GLubyte* GetDataPointer() const {
51 return _dataPointer;
52 }
53 /** @brief Sets the host-side data buffer and repoints each member uniform cache into it */
54 void SetDataPointer(GLubyte* dataPointer);
55
56 /** @brief Returns how many bytes of the cache are uploaded to the UBO */
57 inline GLint usedSize() const {
58 return _usedSize;
59 }
60 /** @brief Sets how many bytes of the cache are uploaded to the UBO */
61 void SetUsedSize(GLint usedSize);
62
63 /** @brief Copies a range of bytes into the data buffer and returns whether it succeeded */
64 bool CopyData(std::uint32_t destIndex, const GLubyte* src, std::uint32_t numBytes);
65 /** @brief Copies the whole used size from the source into the data buffer */
66 inline bool CopyData(const GLubyte* src) {
67 return CopyData(0, src, _usedSize);
68 }
69
70 /** @brief Returns the member uniform cache with the specified name, or `nullptr` if not found */
71 GLUniformCache* GetUniform(StringView name);
72 /** @brief Wrapper around @ref GLUniformBlock::SetBlockBinding() */
73 void SetBlockBinding(GLuint blockBinding);
74
75 #if defined(RHI_GL_PROFILE_ES2)
76 /**
77 @brief OpenGL|ES 2.0 only: uploads each member as a plain loose uniform via glUniform*
78
79 ES2 has no uniform buffer objects, so instead of a UBO bind the block's host-side data is pushed
80 to the program's loose uniforms (each member's location was resolved in GLShaderProgram::ImportReflection).
81 */
82 void CommitAsLooseUniforms();
83 #endif
84
85 private:
86 GLUniformBlock* _uniformBlock;
87 GLubyte* _dataPointer;
88 // Keeps tracks of how much of the cache needs to be uploaded to the UBO
89 GLint _usedSize;
90
91 static const std::uint32_t UniformHashSize = 8;
92 StaticHashMap<String, GLUniformCache, UniformHashSize> _uniformCaches;
93 };
94
95 }