Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
glMemoryManager.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the Software
14is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_GL_MEMORY_MANAGER_H_
34#define DVD_GL_MEMORY_MANAGER_H_
35
37
38namespace Divide {
39namespace GLUtil {
40namespace GLMemory{
41 enum class GLMemoryType {
42 SHADER_BUFFER = 0u,
46 OTHER,
47 COUNT
48 };
49
50 void OnFrameEnd(U64 frameCount);
51
52 struct Block
53 {
54 Byte* _ptr{ nullptr };
55 size_t _offset{ 0u };
56 size_t _size{ 0u };
57 gl46core::GLuint _bufferHandle{ GL_NULL_HANDLE };
58 bool _free{ true };
59
60 bool operator==(const Block &rhs) const = default;
61 };
62
64 {
65 public:
66 explicit Chunk(bool poolAllocations,
67 size_t size,
68 size_t alignment,
69 gl46core::BufferStorageMask storageMask,
70 gl46core::BufferAccessMask accessMask,
71 gl46core::GLenum usage);
72 ~Chunk();
73 void deallocate(const Block& block);
74 [[nodiscard]] bool allocate(size_t size, const char* name, std::pair<bufferPtr, size_t> initialData, Block& blockOut);
75 [[nodiscard]] bool containsBlock(const Block &block) const;
76
77 PROPERTY_RW( gl46core::BufferStorageMask, storageMask, gl46core::BufferStorageMask::GL_NONE_BIT);
78 PROPERTY_RW( gl46core::BufferAccessMask, accessMask, gl46core::BufferAccessMask::GL_NONE_BIT);
79 PROPERTY_RW( gl46core::GLenum, usage, gl46core::GL_NONE);
80 PROPERTY_RW(size_t, alignment, 0u);
81
82 [[nodiscard]] FORCE_INLINE bool poolAllocations() const noexcept { return _poolAllocations; }
83
84 protected:
86 Byte* _memory{ nullptr };
87 const bool _poolAllocations{ false };
88 };
89
91 {
92 public:
93 explicit ChunkAllocator(size_t size) noexcept;
94
95 // if size > mSize, allocate to the next power of 2
96 [[nodiscard]] std::unique_ptr<Chunk> allocate(bool poolAllocations,
97 size_t size,
98 size_t alignment,
99 gl46core::BufferStorageMask storageMask,
100 gl46core::BufferAccessMask accessMask,
101 gl46core::GLenum usage) const;
102
103 private:
104 const size_t _size{ 0u };
105 };
106
108
110 {
111 public:
112 explicit DeviceAllocator(GLMemoryType memoryType) noexcept;
113
114 void init(size_t size);
115 [[nodiscard]] Block allocate(bool poolAllocations,
116 size_t size,
117 size_t alignment,
118 gl46core::BufferStorageMask storageMask,
119 gl46core::BufferAccessMask accessMask,
120 gl46core::GLenum usage,
121 const char* blockName,
122 std::pair<bufferPtr, size_t> initialData);
123 void deallocate(const Block &block) const;
124 void deallocate();
125
126 [[nodiscard]] FORCE_INLINE GLMemoryType glMemoryType() const noexcept { return _memoryType; }
127
128 private:
131 ChunkAllocator_uptr _chunkAllocator{ nullptr };
133 };
134} // namespace GLMemory
135
136void createBuffer( gl46core::GLuint& bufferIdOut, const char* name = nullptr);
137
138void createAndAllocBuffer(size_t bufferSize,
139 gl46core::GLenum usageMask,
140 gl46core::GLuint& bufferIdOut,
141 std::pair<bufferPtr, size_t> initialData,
142 const char* name = nullptr);
143
144Byte* createAndAllocPersistentBuffer(size_t bufferSize,
145 gl46core::BufferStorageMask storageMask,
146 gl46core::BufferAccessMask accessMask,
147 gl46core::GLuint& bufferIdOut,
148 std::pair<bufferPtr, size_t> initialData,
149 const char* name = nullptr);
150
151void freeBuffer( gl46core::GLuint &bufferId, bufferPtr mappedPtr = nullptr);
152
153}; //namespace GLUtil
154}; //namespace Divide
155
156#endif //DVD_GL_MEMORY_MANAGER_H_
#define FWD_DECLARE_MANAGED_CLASS(T)
#define FORCE_INLINE
std::unique_ptr< Chunk > allocate(bool poolAllocations, size_t size, size_t alignment, gl46core::BufferStorageMask storageMask, gl46core::BufferAccessMask accessMask, gl46core::GLenum usage) const
PROPERTY_RW(gl46core::BufferStorageMask, storageMask, gl46core::BufferStorageMask::GL_NONE_BIT)
FORCE_INLINE bool poolAllocations() const noexcept
bool containsBlock(const Block &block) const
bool allocate(size_t size, const char *name, std::pair< bufferPtr, size_t > initialData, Block &blockOut)
PROPERTY_RW(gl46core::GLenum, usage, gl46core::GL_NONE)
PROPERTY_RW(gl46core::BufferAccessMask, accessMask, gl46core::BufferAccessMask::GL_NONE_BIT)
PROPERTY_RW(size_t, alignment, 0u)
void deallocate(const Block &block)
vector< std::unique_ptr< Chunk > > _chunks
FORCE_INLINE GLMemoryType glMemoryType() const noexcept
Block allocate(bool poolAllocations, size_t size, size_t alignment, gl46core::BufferStorageMask storageMask, gl46core::BufferAccessMask accessMask, gl46core::GLenum usage, const char *blockName, std::pair< bufferPtr, size_t > initialData)
void OnFrameEnd(const U64 frameCount)
void createBuffer(gl46core::GLuint &bufferIdOut, const char *name)
void freeBuffer(gl46core::GLuint &bufferId, bufferPtr mappedPtr)
Byte * createAndAllocPersistentBuffer(const size_t bufferSize, const gl46core::BufferStorageMask storageMask, const gl46core::BufferAccessMask accessMask, gl46core::GLuint &bufferIdOut, const std::pair< bufferPtr, size_t > initialData, const char *name)
void createAndAllocBuffer(const size_t bufferSize, const gl46core::GLenum usageMask, gl46core::GLuint &bufferIdOut, const std::pair< bufferPtr, size_t > initialData, const char *name)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
std::byte Byte
std::mutex Mutex
Definition: SharedMutex.h:40
constexpr gl46core::GLuint GL_NULL_HANDLE
Invalid object value. Used to compare handles and determine if they were properly created.
Definition: glResources.h:105
eastl::vector< Type > vector
Definition: Vector.h:42
void * bufferPtr
uint64_t U64
bool operator==(const Block &rhs) const =default