diff options
author | chai <chaifix@163.com> | 2020-10-19 09:13:58 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-10-19 09:13:58 +0800 |
commit | f0807fc44dde14531759306317611bab87c8fccf (patch) | |
tree | 6e78fed61c16a70cda5fa732635f89f9faac9720 /Runtime/Graphics/GpuDataBuffer.h | |
parent | 639b34294ffc20721c66db46e59e07d9100ac4b8 (diff) |
+gamelab proj
Diffstat (limited to 'Runtime/Graphics/GpuDataBuffer.h')
-rw-r--r-- | Runtime/Graphics/GpuDataBuffer.h | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Runtime/Graphics/GpuDataBuffer.h b/Runtime/Graphics/GpuDataBuffer.h new file mode 100644 index 0000000..0d65d3d --- /dev/null +++ b/Runtime/Graphics/GpuDataBuffer.h @@ -0,0 +1,90 @@ +#ifndef GPU_DATABUFFER_H +#define GPU_DATABUFFER_H + +#include <vector> + +#include "../Utilities/Type.h" +#include "../Utilities/Singleton.h" +#include "../Utilities/UtilMacros.h" +#include "../Utilities/Assert.h" +#include "OpenGL.h" + +namespace GPU +{ + + class DataBuffer + { + public: + void Upload(int offset, int size, const void* data); + void* Map(uint32 access); + void* MapRange(int offset, int size, uint32 access); // best performance + void FlushMapedRange(int offset, int size); + + void UnMap(); + + void Orphan(); + + // claim storage of size + void Restore(int size, GLenum usage); + // claim storage of size and fill it + void RestoreWithData(int size, GLenum usage, const void* data); + + GET(int, Size, m_Size); + GET(GLenum, Usage, m_Usage); + GET(GLuint, Handle, m_Handle); + + private: + friend class BufferPool; + + DataBuffer(); + ~DataBuffer(); + + GLuint m_Handle; + int m_Size; + GLenum m_Usage; + }; + + // recycle data buffer + class BufferPool : public Singleton<BufferPool> + { + public: + BufferPool(); + ~BufferPool(); + + void Initialize(); + + DataBuffer* ClaimBuffer(int size, GLenum usage); + void ReleaseBuffer(DataBuffer* buffer); + + void OnEndFrame(); + + private: + + inline int GetSizeClassLimit(int classNdx); + + void UpdatePendingBuffersArray(); + void InsertToLive(DataBuffer* buffer); + uint GetSizeClass(uint bufferSize); + + enum { + kSizeClassBaseLog2 = 10, + kSizeClassStepLog2 = 1, + kSizeClassCount = 7 + }; + + std::vector<DataBuffer*> m_LiveBuffers[kSizeClassCount]; + + }; + + inline int BufferPool::GetSizeClassLimit(int classNdx) + { + // (0, 2^10] 2^11 2^12 2^13 2^14 2^15 INT_MAX + return classNdx + 1 < kSizeClassCount ? (1 << (classNdx*kSizeClassStepLog2 + kSizeClassBaseLog2)) : INT_MAX; + } + + DataBuffer* ClaimBuffer(int size = 0, GLenum usage = GL_ARRAY_BUFFER); + void ReleaseBuffer(DataBuffer* buffer); + +} + +#endif
\ No newline at end of file |