All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
GlBufferPool.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Types.h"
6 #include "SpinLock.h"
7 #include "Graphics.h"
8 #include "Streaming.h"
9 #include <deque>
10 #include <map>
11 #include <string>
12 
13 namespace Eegeo
14 {
15  namespace Rendering
16  {
23  class GlBufferPool : protected Eegeo::NonCopyable
24  {
25  private:
26  typedef std::deque<GLuint> TFreeList;
27 
28  Eegeo::SpinLock m_pSpinLock;
29 
30  TFreeList m_freeIndexBuffers;
31  TFreeList m_freeVertexBuffers;
32 
33  int m_numOfAllocations;
34  int m_numOfReleases;
35  int m_numAllocated;
36  int m_orphanedBufferSize;
37 
38  bool m_canOrphanInitialised;
39  bool m_canOrphanBuffers;
40 
41  void AllocateBuffers(TFreeList& freeBuffers);
42 
43  void ReleaseBuffer(GLuint bufferId, GLint bufferType);
44  void OrphanBuffer(GLuint bufferId, GLint bufferType);
45  void DeleteBuffers(std::deque<GLuint>& toDelete);
46  void DeleteBuffer(GLuint bufferId);
47  GLuint AllocateBuffer(GLint bufferType, size_t bufferSize, const std::string& debugName);
48 
49  bool DetermineCanOrphanBuffers(int orphanedBufferSize) const;
50 
51  void AmortizedReleaseBuffers();
52 
53  struct GLBufferDebugInfo
54  {
55  GLuint bufferId;
56  GLint bufferType;
57  size_t size;
58  std::string name;
59 
60  };
61 
62  std::map<GLuint, GLBufferDebugInfo> m_debugInfo;
63 
64  public:
65  GlBufferPool(bool tryToEnableBufferPooling);
66  virtual ~GlBufferPool();
67 
68  void Update(float dt);
69  GLuint AllocateVertexBuffer(size_t bufferSize, const std::string& debugName);
70  GLuint AllocateIndexBuffer(size_t bufferSize, const std::string& debugName);
71  void ReleaseVertexBuffer(GLuint bufferId);
72  void ReleaseIndexBuffer(GLuint bufferId);
73 
74  void DumpDebugInfo();
75 
76  int GetNumOfAllocations() const;
77  int GetNumOfReleases() const;
78  int GetNumOfFree();
79  int GetNumOfAllocated();
80 
81  static std::string CreateDebugString(const std::string& prefix, int meshIndex, const Streaming::MortonKey& mortonKey, const std::string& materialName = "");
82  };
83  }
84 }