Berserk
GLVaoCache.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_GLVAOCACHE_HPP
29 #define BERSERK_GLVAOCACHE_HPP
30 
31 #include <core/Crc32.hpp>
32 #include <rhi/RHIBuffer.hpp>
34 
35 #include <algorithm>
36 #include <array>
37 #include <unordered_map>
38 #include <utility>
39 
41 
52  std::array<Ref<RHIVertexBuffer>, RHILimits::MAX_VERTEX_BUFFERS> buffers;
55 
56  void Reset() {
58  indices.Reset();
60  }
61 };
62 
67 struct GLVaoKey {
68  std::array<Ref<RHIVertexBuffer>, RHILimits::MAX_VERTEX_BUFFERS> buffers;
72 
74  inline void Setup(const GLVaoDescriptor &descriptor) {
75  Memory::Set(this, 0x0, sizeof(GLVaoKey));
76  std::copy(descriptor.buffers.begin(), descriptor.buffers.end(), buffers.begin());
77  indices = descriptor.indices;
78  declaration = descriptor.declaration;
79  hash = Crc32::Hash(this, sizeof(GLVaoKey));
80  }
81 
83  inline bool operator==(const GLVaoKey &other) const {
84  if (hash != other.hash)
85  return false;
86  return Memory::Compare(this, &other, sizeof(GLVaoKey));
87  };
88 };
89 
95 
96 namespace std {
97 
98  template<>
99  struct hash<BRK_NS::GLVaoKey> {
100  public:
101  size_t operator()(const BRK_NS::GLVaoKey &key) const {
102  return key.hash;
103  }
104  };
105 
106 }// namespace std
107 
109 
119 class GLVaoCache {
120 public:
121  static const uint32 RELEASE_FREQUENCY = 4;
122  static const uint32 TIME_TO_KEEP = 4;
123 
124  BRK_API explicit GLVaoCache(uint32 releaseFrequency = RELEASE_FREQUENCY, uint32 timeToKeep = TIME_TO_KEEP);
126 
128  BRK_API GLuint GetOrCreateVao(const GLVaoDescriptor &descriptor);
129 
131  BRK_API void GC();
132 
133 private:
134  struct GLVaoValue {
135  GLuint handle{};
136  uint32 frameUsed{};
137  };
138 
139 private:
140  void CreateVaoObject(const GLVaoDescriptor &descriptor, GLVaoValue &vao) const;
141  void ReleaseVaoObject(const GLVaoValue &vao) const;
142 
143  std::unordered_map<GLVaoKey, GLVaoValue> mEntries;
144  uint32 mReleaseFrequency;
145  uint32 mTimeToKeep;
146  uint32 mLastRelease = 0;
147  uint32 mCurrentFrame = 0;
148 };
149 
155 
156 #endif//BERSERK_GLVAOCACHE_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_API
Definition: Config.hpp:32
#define BRK_NS
Definition: Config.hpp:50
std::uint32_t uint32
Definition: Typedefs.hpp:44
Crc32 hash value type.
static BRK_API Crc32Hash Hash(const void *buffer, size_t size)
Definition: Crc32.cpp:108
Cache of opengl vertex array objects.
Definition: GLVaoCache.hpp:119
BRK_API ~GLVaoCache()
Definition: GLVaoCache.cpp:37
static const uint32 RELEASE_FREQUENCY
Definition: GLVaoCache.hpp:121
BRK_API GLuint GetOrCreateVao(const GLVaoDescriptor &descriptor)
Definition: GLVaoCache.cpp:44
BRK_API void GC()
Definition: GLVaoCache.cpp:66
static const uint32 TIME_TO_KEEP
Definition: GLVaoCache.hpp:122
BRK_API GLVaoCache(uint32 releaseFrequency=RELEASE_FREQUENCY, uint32 timeToKeep=TIME_TO_KEEP)
Definition: GLVaoCache.cpp:33
static int Compare(const void *a, const void *b, size_t sizeInBytes)
Definition: Memory.hpp:85
static void Set(void *destination, int value, size_t sizeInBytes)
Definition: Memory.hpp:71
void Reset(T *ptr=nullptr)
Definition: Ref.hpp:112
Definition: GLDevice.cpp:46
Definition: TQuat.hpp:431
Descriptor of vertex array object.
Definition: GLVaoCache.hpp:51
std::array< Ref< RHIVertexBuffer >, RHILimits::MAX_VERTEX_BUFFERS > buffers
Definition: GLVaoCache.hpp:52
Ref< RHIVertexDeclaration > declaration
Definition: GLVaoCache.hpp:54
void Reset()
Definition: GLVaoCache.hpp:56
Ref< RHIIndexBuffer > indices
Definition: GLVaoCache.hpp:53
Key of vertex array object in the cache.
Definition: GLVaoCache.hpp:67
Crc32Hash hash
Definition: GLVaoCache.hpp:71
Ref< RHIIndexBuffer > indices
Definition: GLVaoCache.hpp:69
void Setup(const GLVaoDescriptor &descriptor)
Initialize key from descriptor.
Definition: GLVaoCache.hpp:74
bool operator==(const GLVaoKey &other) const
Fast compare op.
Definition: GLVaoCache.hpp:83
std::array< Ref< RHIVertexBuffer >, RHILimits::MAX_VERTEX_BUFFERS > buffers
Definition: GLVaoCache.hpp:68
Ref< RHIVertexDeclaration > declaration
Definition: GLVaoCache.hpp:70
static const uint32 MAX_VERTEX_BUFFERS
Definition: RHIDefs.hpp:317
size_t operator()(const BRK_NS::GLVaoKey &key) const
Definition: GLVaoCache.hpp:101