Debian Codegraph @

Sources/nCine/Graphics/RHI/GL/GLHashMap.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 /** @brief Key type, an OpenGL target enum */
13 using key_t = GLenum;
14 /** @brief Value type, an OpenGL object id */
15 using value_t = GLuint;
16
17 /**
18 @brief Naive fixed-size hashmap of OpenGL targets to object ids
19
20 Stores one object id per bucket, where `MappingFunc` maps an OpenGL target
21 (the key) to a bucket index. Used to cache the currently bound object id for
22 each target. The number of buckets `S` must match the range of the mapping
23 function.
24 */
25 template<std::uint32_t S, class MappingFunc>
26 class GLHashMap
27 {
28 public:
29 GLHashMap();
30 /** @brief Returns the bucket for a target, mapping the key through the mapping function */
31 value_t& operator[](key_t key);
32
33 /** @brief Overwrites every bucket with the same value (used to drop the cache when external code changed the bindings) */
34 void SetAll(value_t value) {
35 for (std::uint32_t i = 0; i < S; i++) {
36 _buckets[i] = value;
37 }
38 }
39
40 private:
41 value_t _buckets[S];
42 MappingFunc mappingFunc;
43 };
44
45 template<std::uint32_t S, class MappingFunc>
46 GLHashMap<S, MappingFunc>::GLHashMap()
47 {
48 // Initializing with a null OpenGL object id
49 for (std::uint32_t i = 0; i < S; i++)
50 _buckets[i] = 0;
51 }
52
53 template<std::uint32_t S, class MappingFunc>
54 inline value_t& GLHashMap<S, MappingFunc>::operator[](key_t key)
55 {
56 return _buckets[mappingFunc(key)];
57 }
58
59 /**
60 @brief Maps OpenGL buffer object targets to bucket indices
61
62 Mapping function for @ref GLHashMap that turns buffer object targets
63 (`GL_ARRAY_BUFFER`, `GL_ELEMENT_ARRAY_BUFFER`, ...) into contiguous indices.
64 */
65 class GLBufferObjectMappingFunc
66 {
67 public:
68 /** @brief Number of distinct buffer object targets, i.e. the required bucket count */
69 static const std::uint32_t Size = 6;
70 /** @brief Returns the bucket index for a buffer object target */
71 inline std::uint32_t operator()(key_t key) const
72 {
73 std::uint32_t value = 0;
74
75 switch (key) {
76 case GL_ARRAY_BUFFER:
77 value = 0;
78 break;
79 case GL_ELEMENT_ARRAY_BUFFER:
80 value = 1;
81 break;
82 case GL_UNIFORM_BUFFER:
83 value = 2;
84 break;
85 #if defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_0
86 // Pixel buffer objects are OpenGL ES 3.0 (and desktop GL). A real ES 2.0 target such as PS Vita's
87 // vitaGL neither declares these enums nor ever binds a PBO, so the cases exist only where available.
88 case GL_PIXEL_PACK_BUFFER:
89 value = 3;
90 break;
91 case GL_PIXEL_UNPACK_BUFFER:
92 value = 4;
93 break;
94 #endif
95 #if defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_2
96 case GL_TEXTURE_BUFFER:
97 value = 5;
98 break;
99 #endif
100 default:
101 LOGF("No available case to handle buffer object target: 0x{:x}", key);
102 break;
103 }
104
105 return value;
106 }
107 };
108
109 /**
110 @brief Maps OpenGL texture targets to bucket indices
111
112 Mapping function for @ref GLHashMap that turns texture targets
113 (`GL_TEXTURE_2D`, `GL_TEXTURE_3D`, ...) into contiguous indices.
114 */
115 class GLTextureMappingFunc
116 {
117 public:
118 /** @brief Number of distinct texture targets, i.e. the required bucket count */
119 static const std::uint32_t Size = 4;
120 /** @brief Returns the bucket index for a texture target */
121 inline std::uint32_t operator()(key_t key) const
122 {
123 std::uint32_t value = 0;
124
125 switch (key) {
126 #if defined(RHI_GL_PROFILE_CORE) // not available in OpenGL ES
127 case GL_TEXTURE_1D:
128 value = 0;
129 break;
130 #endif
131 case GL_TEXTURE_2D:
132 value = 1;
133 break;
134 #if defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_0
135 // 3D textures are OpenGL ES 3.0 (and desktop GL), unavailable and never bound on a real ES 2.0
136 // target such as PS Vita's vitaGL.
137 case GL_TEXTURE_3D:
138 value = 2;
139 break;
140 #endif
141 #if defined(RHI_GL_PROFILE_CORE) || GL_ES_VERSION_3_2
142 case GL_TEXTURE_BUFFER:
143 value = 3;
144 break;
145 #endif
146 default:
147 LOGF("No available case to handle texture target: 0x{:x}", key);
148 break;
149 }
150
151 return value;
152 }
153 };
154
155 }