All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
VideoMemoryMonitor.h
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 
6 #include "Graphics.h"
7 #include "Types.h"
8 #include "SpinLock.h"
9 
10 #define Eegeo_LOG_VIDEO_MEMORY_STATS
11 
12 #if defined(Eegeo_LOG_VIDEO_MEMORY_STATS)
13 #define Eegeo_TEXTURE_MEMORY_ADD(textureID, internalFormat, width, height, faceCount, isMipped) \
14  Eegeo::VideoMemoryMonitor::Get().AddTexture((textureID), Eegeo::VideoMemoryMonitor::EstimateGLTextureSize((internalFormat), (width), (height), (faceCount), (isMipped)))
15 #define Eegeo_TEXTURE_MEMORY_REMOVE(textureID) \
16  Eegeo::VideoMemoryMonitor::Get().RemoveTexture((textureID))
17 
18 #define Eegeo_BUFFER_MEMORY_ADD(bufferID, bufferSize) \
19  Eegeo::VideoMemoryMonitor::Get().AddBuffer((bufferID), (bufferSize))
20 #define Eegeo_BUFFER_MEMORY_REMOVE(bufferID) \
21  Eegeo::VideoMemoryMonitor::Get().RemoveBuffer((bufferID))
22 
23 #else
24 
25 #define Eegeo_TEXTURE_MEMORY_ADD(textureID, internalFormat, width, height, faceCount, isMipped)
26 #define Eegeo_TEXTURE_MEMORY_REMOVE(textureID)
27 
28 #define Eegeo_BUFFER_MEMORY_ADD(bufferID, bufferSize)
29 #define Eegeo_BUFFER_MEMORY_REMOVE(bufferID)
30 
31 #endif
32 
33 namespace Eegeo
34 {
35 
37  {
38  public:
39 
40  void AddNameWithUsage(uint32_t name, size_t usage)
41  {
42  SpinLockAutoLock lock(m_lock);
43 
44  if (m_namesToUsage.find(name) == m_namesToUsage.end())
45  {
46  m_namesToUsage.insert(std::make_pair(name, usage));
47  }
48  else
49  {
50  m_namesToUsage[name] = usage;
51  }
52  }
53 
54  void RemoveName(uint32_t name)
55  {
56  SpinLockAutoLock lock(m_lock);
57  m_namesToUsage.erase(name);
58  }
59 
60  size_t GetCount() const
61  {
62  SpinLockAutoLock lock(m_lock);
63  return m_namesToUsage.size();
64  }
65 
66  size_t GetTotal() const
67  {
68  SpinLockAutoLock lock(m_lock);
69  size_t total = 0;
70 
71  for (std::map<uint32_t, size_t>::const_iterator it = m_namesToUsage.begin(); it != m_namesToUsage.end(); ++it)
72  {
73  total += it->second;
74  }
75 
76  return total;
77  }
78 
79  private:
80 
81  mutable SpinLock m_lock;
82  std::map<uint32_t, size_t> m_namesToUsage;
83  };
84 
86  {
87  public:
88 
89  void AddTexture(uint32_t texture, size_t size);
90  void RemoveTexture(uint32_t texture);
91 
92  void AddBuffer(uint32_t buffer, size_t size);
93  void RemoveBuffer(uint32_t buffer);
94 
95  static VideoMemoryMonitor& Get()
96  {
97  static VideoMemoryMonitor instance;
98  return instance;
99  }
100 
101  std::string GenerateReport();
102 
103  size_t GetBufferByteCount() const { return m_buffers.GetTotal(); }
104  size_t GetBufferCount() const { return m_buffers.GetCount(); }
105  size_t GetTextureByteCount() const { return m_textures.GetTotal(); }
106  size_t GetTextureCount() const { return m_textures.GetCount(); }
107 
108  static size_t EstimateGLTextureSize(int internalFormat, uint32_t width, uint32_t height, uint32_t faceCount, bool isMipped);
109 
110  private:
111 
112  static size_t GetBitsPerTexelBlock(int internalFormat, int &blockWidthInTexels, int &blockHeightInTexels);
113 
114  GLUsageTracker m_textures;
115  GLUsageTracker m_buffers;
116  };
117 
118 };