gfxGLCircularVolatileBuffer.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #ifndef GL_CIRCULAR_VOLATILE_BUFFER_H
  2. #define GL_CIRCULAR_VOLATILE_BUFFER_H
  3. #include "gfx/gl/gfxGLDevice.h"
  4. #include "gfx/gl/gfxGLUtils.h"
  5. class GLFenceRange
  6. {
  7. public:
  8. GLFenceRange() : mStart(0), mEnd(0), mSync(0)
  9. {
  10. }
  11. ~GLFenceRange()
  12. {
  13. //the order of creation/destruction of static variables is indetermined... depends on detail of the build
  14. //looks like for some reason on windows + sdl + opengl the order make invalid / wrong the process TODO: Refactor -LAR
  15. //AssertFatal( mSync == 0, "");
  16. }
  17. void init(U32 start, U32 end)
  18. {
  19. PROFILE_SCOPE(GFXGLQueryFence_issue);
  20. mStart = start;
  21. mEnd = end;
  22. mSync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
  23. }
  24. bool checkOverlap(U32 start, U32 end)
  25. {
  26. if( mStart < end && start < mEnd )
  27. return true;
  28. return false;
  29. }
  30. void wait()
  31. {
  32. PROFILE_SCOPE(GFXGLQueryFence_block);
  33. GLbitfield waitFlags = 0;
  34. GLuint64 waitDuration = 0;
  35. while( 1 )
  36. {
  37. GLenum waitRet = glClientWaitSync( mSync, waitFlags, waitDuration );
  38. if( waitRet == GL_ALREADY_SIGNALED || waitRet == GL_CONDITION_SATISFIED )
  39. {
  40. break;
  41. }
  42. if( waitRet == GL_WAIT_FAILED )
  43. {
  44. AssertFatal(0, "GLSync failed.");
  45. break;
  46. }
  47. waitFlags = GL_SYNC_FLUSH_COMMANDS_BIT;
  48. waitDuration = scOneSecondInNanoSeconds;
  49. }
  50. glDeleteSync(mSync);
  51. mSync = 0;
  52. }
  53. void swap( GLFenceRange &r )
  54. {
  55. GLFenceRange temp;
  56. temp = *this;
  57. *this = r;
  58. r = temp;
  59. }
  60. protected:
  61. U32 mStart, mEnd;
  62. GLsync mSync;
  63. static const GLuint64 scOneSecondInNanoSeconds = 1000000000;
  64. GLFenceRange( const GLFenceRange &);
  65. GLFenceRange& operator=(const GLFenceRange &r)
  66. {
  67. mStart = r.mStart;
  68. mEnd = r.mEnd;
  69. mSync = r.mSync;
  70. return *this;
  71. }
  72. };
  73. class GLOrderedFenceRangeManager
  74. {
  75. public:
  76. ~GLOrderedFenceRangeManager( )
  77. {
  78. //the order of creation/destruction of static variables is indetermined... depends on detail of the build
  79. //looks like for some reason on windows + sdl + opengl the order make invalid / wrong the process TODO: Refactor -LAR
  80. //waitAllRanges( );
  81. }
  82. void protectOrderedRange( U32 start, U32 end )
  83. {
  84. mFenceRanges.increment();
  85. GLFenceRange &range = mFenceRanges.last();
  86. range.init( start, end );
  87. }
  88. void waitFirstRange( U32 start, U32 end )
  89. {
  90. if( !mFenceRanges.size() || !mFenceRanges[0].checkOverlap( start, end ) )
  91. return;
  92. mFenceRanges[0].wait();
  93. mFenceRanges.pop_front();
  94. }
  95. void waitOverlapRanges( U32 start, U32 end )
  96. {
  97. for( U32 i = 0; i < mFenceRanges.size(); ++i )
  98. {
  99. if( !mFenceRanges[i].checkOverlap( start, end ) )
  100. continue;
  101. mFenceRanges[i].wait();
  102. mFenceRanges.erase(i);
  103. }
  104. }
  105. void waitAllRanges()
  106. {
  107. for( int i = 0; i < mFenceRanges.size(); ++i )
  108. mFenceRanges[i].wait();
  109. mFenceRanges.clear();
  110. }
  111. protected:
  112. Vector<GLFenceRange> mFenceRanges;
  113. };
  114. class GLCircularVolatileBuffer
  115. {
  116. public:
  117. GLCircularVolatileBuffer(GLuint binding)
  118. : mBinding(binding), mBufferName(0), mBufferPtr(NULL), mBufferSize(0), mBufferFreePos(0), mCurrectUsedRangeStart(0)
  119. {
  120. init();
  121. }
  122. ~GLCircularVolatileBuffer()
  123. {
  124. glDeleteBuffers(1, &mBufferName);
  125. }
  126. void init()
  127. {
  128. glGenBuffers(1, &mBufferName);
  129. PRESERVE_BUFFER( mBinding );
  130. glBindBuffer(mBinding, mBufferName);
  131. const U32 cSizeInMB = 10;
  132. mBufferSize = (cSizeInMB << 20);
  133. if( GFXGL->mCapabilities.bufferStorage )
  134. {
  135. const GLbitfield flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
  136. glBufferStorage(mBinding, mBufferSize, NULL, flags);
  137. mBufferPtr = glMapBufferRange(mBinding, 0, mBufferSize, flags);
  138. }
  139. else
  140. {
  141. glBufferData(mBinding, mBufferSize, NULL, GL_DYNAMIC_DRAW);
  142. }
  143. }
  144. struct
  145. {
  146. U32 mOffset = 0;
  147. U32 mSize = 0;
  148. }_getBufferData;
  149. void lock(const U32 size, U32 offsetAlign, U32& outOffset, void*& outPtr)
  150. {
  151. if (!size)
  152. {
  153. AssertFatal(0, "GLCircularVolatileBuffer::lock - size must be > 0");
  154. outOffset = 0;
  155. outPtr = nullptr;
  156. return;
  157. }
  158. // Align free pos first (before wraparound check)
  159. if (offsetAlign)
  160. {
  161. mBufferFreePos = ((mBufferFreePos + offsetAlign - 1) / offsetAlign) * offsetAlign;
  162. }
  163. // If the size won't fit from current pos to end, wrap around
  164. if (mBufferFreePos + size > mBufferSize)
  165. {
  166. // Protect the remaining space
  167. if (mBufferFreePos < mBufferSize)
  168. mUsedRanges.push_back(UsedRange(mBufferFreePos, mBufferSize - 1));
  169. // Reset free pos
  170. mBufferFreePos = 0;
  171. // Realign after wrap
  172. if (offsetAlign)
  173. {
  174. mBufferFreePos = ((mBufferFreePos + offsetAlign - 1) / offsetAlign) * offsetAlign;
  175. }
  176. // Now check for overlaps *after* wrapping
  177. mLockManager.waitOverlapRanges(mBufferFreePos, mBufferFreePos + size - 1);
  178. }
  179. else
  180. {
  181. // Normal range wait
  182. mLockManager.waitOverlapRanges(mBufferFreePos, mBufferFreePos + size - 1);
  183. }
  184. outOffset = mBufferFreePos;
  185. if (GFXGL->mCapabilities.bufferStorage)
  186. {
  187. outPtr = static_cast<U8*>(mBufferPtr) + mBufferFreePos;
  188. }
  189. else if (GFXGL->glUseMap())
  190. {
  191. PRESERVE_BUFFER(mBinding);
  192. glBindBuffer(mBinding, mBufferName);
  193. const GLbitfield access = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT;
  194. outPtr = glMapBufferRange(mBinding, outOffset, size, access);
  195. }
  196. else
  197. {
  198. _getBufferData.mOffset = outOffset;
  199. _getBufferData.mSize = size;
  200. outPtr = mFrameAllocator.lock(size);
  201. }
  202. mBufferFreePos += size;
  203. //align 4bytes
  204. mBufferFreePos = ((mBufferFreePos + 4 - 1) / 4) * 4;
  205. }
  206. void unlock()
  207. {
  208. if( GFXGL->mCapabilities.bufferStorage )
  209. {
  210. return;
  211. }
  212. else if( GFXGL->glUseMap() )
  213. {
  214. PRESERVE_BUFFER( mBinding );
  215. glBindBuffer(mBinding, mBufferName);
  216. glUnmapBuffer(mBinding);
  217. }
  218. else
  219. {
  220. PRESERVE_BUFFER( mBinding );
  221. glBindBuffer(mBinding, mBufferName);
  222. glBufferSubData( mBinding, _getBufferData.mOffset, _getBufferData.mSize, mFrameAllocator.getlockedPtr() );
  223. _getBufferData.mOffset = 0;
  224. _getBufferData.mSize = 0;
  225. mFrameAllocator.unlock();
  226. }
  227. }
  228. U32 getHandle() const { return mBufferName; }
  229. void protectUsedRange()
  230. {
  231. for( int i = 0; i < mUsedRanges.size(); ++i )
  232. {
  233. mLockManager.protectOrderedRange( mUsedRanges[i].start, mUsedRanges[i].end );
  234. }
  235. mUsedRanges.clear();
  236. if( mCurrectUsedRangeStart < mBufferFreePos )
  237. {
  238. mLockManager.protectOrderedRange( mCurrectUsedRangeStart, mBufferFreePos-1 );
  239. mCurrectUsedRangeStart = mBufferFreePos;
  240. }
  241. }
  242. protected:
  243. GLuint mBinding;
  244. GLuint mBufferName;
  245. void *mBufferPtr;
  246. U32 mBufferSize;
  247. U32 mBufferFreePos;
  248. U32 mCurrectUsedRangeStart;
  249. GLOrderedFenceRangeManager mLockManager;
  250. FrameAllocatorLockableHelper mFrameAllocator;
  251. struct UsedRange
  252. {
  253. UsedRange(U32 _start = 0, U32 _end = 0)
  254. : start(_start), end(_end)
  255. {
  256. }
  257. U32 start, end;
  258. };
  259. Vector<UsedRange> mUsedRanges;
  260. };
  261. #endif