Avara3D 0.2.0
C++ API reference
OGLMemoryTracker.h
1//
2// OGLMemoryTracker.h
3// avara3d
4//
5
6#ifndef AVARA3D_RENDER_BACKEND_OPENGL_OGLMEMORYTRACKER_H
7#define AVARA3D_RENDER_BACKEND_OPENGL_OGLMEMORYTRACKER_H
8
9#include <cstdint>
10#include <string>
11#include <unordered_map>
12
13#include "a3d/profile/FrameStats.h"
14#include "a3d/render/backend/opengl/GLTypes.h"
15
16namespace a3d {
17
18 class OGLMemoryTracker {
19
20 public:
21 // [Internal Types]
22
23 enum class ObjectNamespace : std::uint8_t {
24 Buffer,
25 Texture,
26 Renderbuffer, // unused
27 Synthetic,
28 };
29
30 enum class Source : std::uint8_t {
31 A3D,
32 ImGui,
33 };
34
35 enum class Category : std::uint8_t {
36 Texture,
37 VertexBuffer,
38 IndexBuffer,
39 UniformBuffer,
40 RenderTarget,
41 Other,
42 };
43
44 struct AllocationKey {
45 ObjectNamespace type {};
46 gl::uint_t id {};
47
48 friend bool operator==(const AllocationKey&, const AllocationKey&) = default;
49 };
50
51 // [Internal Member Functions]
52
53 void setAllocation(AllocationKey key,
54 Source source,
55 Category category,
56 std::uint64_t bytes,
57 std::string label = {});
58
59 void removeAllocation(AllocationKey key);
60 void clearSource(Source source);
61
62 RenderMemoryStats stats() const;
63
64 private:
65 // [Private Types]
66
67 struct AllocationKeyHash {
68 std::size_t operator()(const AllocationKey& key) const noexcept;
69 };
70
71 struct Entry {
72 Source source {};
73 Category category {};
74 std::uint64_t bytes {};
75 std::string label {};
76 };
77
78 // [Private Member Functions]
79
80 std::uint64_t currentTotalBytes() const;
81
82 // [Private Member Variables]
83
84 std::unordered_map<AllocationKey, Entry, AllocationKeyHash> _allocations;
85 std::uint64_t _peakTotalBytes {0};
86 };
87
88}
89
90#endif // AVARA3D_RENDER_BACKEND_OPENGL_OGLMEMORYTRACKER_H