StreamBuffer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /**
  2. * Copyright (c) 2006-2020 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "common/config.h"
  21. #include "StreamBuffer.h"
  22. #include "OpenGL.h"
  23. #include "FenceSync.h"
  24. #include "graphics/Volatile.h"
  25. #include "common/Exception.h"
  26. #include "common/memory.h"
  27. #include <vector>
  28. #include <algorithm>
  29. namespace love
  30. {
  31. namespace graphics
  32. {
  33. namespace opengl
  34. {
  35. // Typically this should be 3 frames, but we only do per-frame syncing right now
  36. // so we add an extra frame to reduce the (small) chance of stalls.
  37. static const int BUFFER_FRAMES = 4;
  38. class StreamBufferClientMemory final : public love::graphics::StreamBuffer
  39. {
  40. public:
  41. StreamBufferClientMemory(BufferType mode, size_t size)
  42. : love::graphics::StreamBuffer(mode, size)
  43. , data(nullptr)
  44. {
  45. try
  46. {
  47. data = new uint8[size];
  48. }
  49. catch (std::exception &)
  50. {
  51. throw love::Exception("Out of memory.");
  52. }
  53. }
  54. virtual ~StreamBufferClientMemory()
  55. {
  56. delete[] data;
  57. }
  58. MapInfo map(size_t /*minsize*/) override
  59. {
  60. return MapInfo(data, bufferSize);
  61. }
  62. size_t unmap(size_t /*usedsize*/) override
  63. {
  64. return (size_t) data;
  65. }
  66. void markUsed(size_t /*usedsize*/) override { }
  67. ptrdiff_t getHandle() const override { return 0; }
  68. private:
  69. uint8 *data;
  70. }; // StreamBufferClientMemory
  71. class StreamBufferSubDataOrphan final : public love::graphics::StreamBuffer, public Volatile
  72. {
  73. public:
  74. StreamBufferSubDataOrphan(BufferType mode, size_t size)
  75. : love::graphics::StreamBuffer(mode, size)
  76. , vbo(0)
  77. , glMode(OpenGL::getGLBufferType(mode))
  78. , data(nullptr)
  79. , orphan(false)
  80. {
  81. try
  82. {
  83. data = new uint8[size];
  84. }
  85. catch (std::exception &)
  86. {
  87. throw love::Exception("Out of memory.");
  88. }
  89. loadVolatile();
  90. }
  91. virtual ~StreamBufferSubDataOrphan()
  92. {
  93. unloadVolatile();
  94. delete[] data;
  95. }
  96. MapInfo map(size_t /*minsize*/) override
  97. {
  98. if (orphan)
  99. {
  100. orphan = false;
  101. frameGPUReadOffset = 0;
  102. gl.bindBuffer(mode, vbo);
  103. glBufferData(glMode, bufferSize, nullptr, GL_STREAM_DRAW);
  104. }
  105. return MapInfo(data, bufferSize - frameGPUReadOffset);
  106. }
  107. size_t unmap(size_t usedsize) override
  108. {
  109. gl.bindBuffer(mode, vbo);
  110. glBufferSubData(glMode, frameGPUReadOffset, usedsize, data);
  111. return frameGPUReadOffset;
  112. }
  113. void markUsed(size_t usedsize) override
  114. {
  115. frameGPUReadOffset += usedsize;
  116. }
  117. void nextFrame() override
  118. {
  119. // Orphan the buffer before its first use in the next frame.
  120. frameGPUReadOffset = 0;
  121. orphan = true;
  122. }
  123. ptrdiff_t getHandle() const override { return vbo; }
  124. bool loadVolatile() override
  125. {
  126. if (vbo != 0)
  127. return true;
  128. glGenBuffers(1, &vbo);
  129. gl.bindBuffer(mode, vbo);
  130. glBufferData(glMode, bufferSize, nullptr, GL_STREAM_DRAW);
  131. frameGPUReadOffset = 0;
  132. orphan = false;
  133. return true;
  134. }
  135. void unloadVolatile() override
  136. {
  137. if (vbo == 0)
  138. return;
  139. gl.deleteBuffer(vbo);
  140. vbo = 0;
  141. }
  142. protected:
  143. GLuint vbo;
  144. GLenum glMode;
  145. uint8 *data;
  146. bool orphan;
  147. }; // StreamBufferSubDataOrphan
  148. class StreamBufferSync : public love::graphics::StreamBuffer
  149. {
  150. public:
  151. StreamBufferSync(BufferType type, size_t size)
  152. : love::graphics::StreamBuffer(type, size)
  153. , frameIndex(0)
  154. , syncs()
  155. {}
  156. virtual ~StreamBufferSync() {}
  157. void nextFrame() override
  158. {
  159. // Insert a GPU fence for this frame's section of the data, we'll wait
  160. // for it when we try to map that data for writing in subsequent frames.
  161. syncs[frameIndex].fence();
  162. frameIndex = (frameIndex + 1) % BUFFER_FRAMES;
  163. frameGPUReadOffset = 0;
  164. }
  165. void markUsed(size_t usedsize) override
  166. {
  167. // We insert a fence for all data from this frame at the end of the
  168. // frame (in nextFrame), rather than doing anything more fine-grained.
  169. frameGPUReadOffset += usedsize;
  170. }
  171. protected:
  172. int frameIndex;
  173. FenceSync syncs[BUFFER_FRAMES];
  174. }; // StreamBufferSync
  175. class StreamBufferMapSync final : public StreamBufferSync, public Volatile
  176. {
  177. public:
  178. StreamBufferMapSync(BufferType type, size_t size)
  179. : StreamBufferSync(type, size)
  180. , vbo(0)
  181. , glMode(OpenGL::getGLBufferType(mode))
  182. {
  183. loadVolatile();
  184. }
  185. ~StreamBufferMapSync()
  186. {
  187. unloadVolatile();
  188. }
  189. MapInfo map(size_t /*minsize*/) override
  190. {
  191. gl.bindBuffer(mode, vbo);
  192. // Make sure this frame's section of the buffer is done being used.
  193. syncs[frameIndex].cpuWait();
  194. MapInfo info;
  195. info.size = bufferSize - frameGPUReadOffset;
  196. GLbitfield flags = GL_MAP_WRITE_BIT | GL_MAP_FLUSH_EXPLICIT_BIT | GL_MAP_UNSYNCHRONIZED_BIT;
  197. size_t mapoffset = (frameIndex * bufferSize) + frameGPUReadOffset;
  198. info.data = (uint8 *) glMapBufferRange(glMode, mapoffset, info.size, flags);
  199. return info;
  200. }
  201. size_t unmap(size_t usedsize) override
  202. {
  203. gl.bindBuffer(mode, vbo);
  204. glFlushMappedBufferRange(glMode, 0, usedsize);
  205. glUnmapBuffer(glMode);
  206. return (frameIndex * bufferSize) + frameGPUReadOffset;
  207. }
  208. ptrdiff_t getHandle() const override { return vbo; }
  209. bool loadVolatile() override
  210. {
  211. if (vbo != 0)
  212. return true;
  213. glGenBuffers(1, &vbo);
  214. gl.bindBuffer(mode, vbo);
  215. glBufferData(glMode, bufferSize * BUFFER_FRAMES, nullptr, GL_STREAM_DRAW);
  216. frameGPUReadOffset = 0;
  217. frameIndex = 0;
  218. return true;
  219. }
  220. void unloadVolatile() override
  221. {
  222. if (vbo != 0)
  223. {
  224. gl.deleteBuffer(vbo);
  225. vbo = 0;
  226. }
  227. for (FenceSync &sync : syncs)
  228. sync.cleanup();
  229. }
  230. private:
  231. GLuint vbo;
  232. GLenum glMode;
  233. }; // StreamBufferMapSync
  234. class StreamBufferPersistentMapSync final : public StreamBufferSync, public Volatile
  235. {
  236. public:
  237. // Coherent mapping is supposedly faster on intel/nvidia aside from a couple
  238. // old nvidia GPUs.
  239. StreamBufferPersistentMapSync(BufferType type, size_t size, bool coherent = true)
  240. : StreamBufferSync(type, size)
  241. , vbo(0)
  242. , glMode(OpenGL::getGLBufferType(mode))
  243. , data(nullptr)
  244. , coherent(coherent)
  245. {
  246. loadVolatile();
  247. }
  248. ~StreamBufferPersistentMapSync()
  249. {
  250. unloadVolatile();
  251. }
  252. MapInfo map(size_t /*minsize*/) override
  253. {
  254. // Make sure this frame's section of the buffer is done being used.
  255. syncs[frameIndex].cpuWait();
  256. MapInfo info;
  257. info.size = bufferSize - frameGPUReadOffset;
  258. info.data = data + (frameIndex * bufferSize) + frameGPUReadOffset;
  259. return info;
  260. }
  261. size_t unmap(size_t usedsize) override
  262. {
  263. size_t offset = (frameIndex * bufferSize) + frameGPUReadOffset;
  264. if (!coherent)
  265. {
  266. gl.bindBuffer(mode, vbo);
  267. glFlushMappedBufferRange(glMode, offset, usedsize);
  268. }
  269. return offset;
  270. }
  271. ptrdiff_t getHandle() const override { return vbo; }
  272. bool loadVolatile() override
  273. {
  274. if (vbo != 0)
  275. return true;
  276. glGenBuffers(1, &vbo);
  277. gl.bindBuffer(mode, vbo);
  278. GLbitfield storageflags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT;
  279. GLbitfield mapflags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT;
  280. storageflags |= (coherent ? GL_MAP_COHERENT_BIT : 0);
  281. mapflags |= (coherent ? GL_MAP_COHERENT_BIT : GL_MAP_FLUSH_EXPLICIT_BIT);
  282. glBufferStorage(glMode, bufferSize * BUFFER_FRAMES, nullptr, storageflags);
  283. data = (uint8 *) glMapBufferRange(glMode, 0, bufferSize * BUFFER_FRAMES, mapflags);
  284. frameGPUReadOffset = 0;
  285. frameIndex = 0;
  286. return true;
  287. }
  288. void unloadVolatile() override
  289. {
  290. if (vbo != 0)
  291. {
  292. gl.bindBuffer(mode, vbo);
  293. glUnmapBuffer(glMode);
  294. gl.deleteBuffer(vbo);
  295. vbo = 0;
  296. }
  297. for (FenceSync &sync : syncs)
  298. sync.cleanup();
  299. }
  300. private:
  301. GLuint vbo;
  302. GLenum glMode;
  303. uint8 *data;
  304. bool coherent;
  305. }; // StreamBufferPersistentMapSync
  306. class StreamBufferPinnedMemory final : public StreamBufferSync, public Volatile
  307. {
  308. public:
  309. StreamBufferPinnedMemory(BufferType type, size_t size)
  310. : StreamBufferSync(type, size)
  311. , vbo(0)
  312. , glMode(OpenGL::getGLBufferType(mode))
  313. , data(nullptr)
  314. , alignedSize(0)
  315. {
  316. size_t alignment = getPageSize();
  317. alignedSize = alignUp(size * BUFFER_FRAMES, alignment);
  318. if (!alignedMalloc((void **) &data, alignedSize, alignment))
  319. throw love::Exception("Out of memory.");
  320. if (!loadVolatile())
  321. {
  322. ptrdiff_t pointer = (ptrdiff_t) data;
  323. alignedFree(data);
  324. throw love::Exception("AMD Pinned Memory StreamBuffer implementation failed to create buffer (address: %p, alignment: %ld, aiigned size: %ld)", pointer, alignment, alignedSize);
  325. }
  326. }
  327. ~StreamBufferPinnedMemory()
  328. {
  329. unloadVolatile();
  330. alignedFree(data);
  331. }
  332. MapInfo map(size_t /*minsize*/) override
  333. {
  334. // Make sure this frame's section of the buffer is done being used.
  335. syncs[frameIndex].cpuWait();
  336. MapInfo info;
  337. info.size = bufferSize - frameGPUReadOffset;
  338. info.data = data + (frameIndex * bufferSize) + frameGPUReadOffset;
  339. return info;
  340. }
  341. size_t unmap(size_t /*usedsize*/) override
  342. {
  343. size_t offset = (frameIndex * bufferSize) + frameGPUReadOffset;
  344. return offset;
  345. }
  346. ptrdiff_t getHandle() const override { return vbo; }
  347. bool loadVolatile() override
  348. {
  349. if (vbo != 0)
  350. return true;
  351. glGenBuffers(1, &vbo);
  352. while (glGetError() != GL_NO_ERROR)
  353. /* Clear errors. */;
  354. glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, vbo);
  355. glBufferData(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, alignedSize, data, GL_STREAM_DRAW);
  356. if (glGetError() != GL_NO_ERROR)
  357. {
  358. gl.deleteBuffer(vbo);
  359. vbo = 0;
  360. return false;
  361. }
  362. frameGPUReadOffset = 0;
  363. frameIndex = 0;
  364. return true;
  365. }
  366. void unloadVolatile() override
  367. {
  368. if (vbo != 0)
  369. {
  370. // Make sure the GPU has completed all work before freeing the
  371. // memory. glFlush+sync.cpuWait doesn't seem to be enough.
  372. glFinish();
  373. gl.bindBuffer(mode, vbo);
  374. gl.deleteBuffer(vbo);
  375. vbo = 0;
  376. }
  377. for (FenceSync &sync : syncs)
  378. sync.cleanup();
  379. }
  380. private:
  381. GLuint vbo;
  382. GLenum glMode;
  383. uint8 *data;
  384. size_t alignedSize;
  385. }; // StreamBufferPinnedMemory
  386. love::graphics::StreamBuffer *CreateStreamBuffer(BufferType mode, size_t size)
  387. {
  388. if (gl.isCoreProfile())
  389. {
  390. if (!gl.bugs.clientWaitSyncStalls)
  391. {
  392. // AMD's pinned memory seems to be faster than persistent mapping,
  393. // on AMD GPUs.
  394. if (GLAD_AMD_pinned_memory)
  395. {
  396. try
  397. {
  398. return new StreamBufferPinnedMemory(mode, size);
  399. }
  400. catch (love::Exception &e)
  401. {
  402. printf("Failed creating Pinned Memory StreamBuffer: %s\n", e.what());
  403. }
  404. }
  405. if (GLAD_VERSION_4_4 || GLAD_ARB_buffer_storage)
  406. return new StreamBufferPersistentMapSync(mode, size);
  407. // Most modern drivers have a separate internal thread which queues
  408. // GL commands for the GPU. The queue causes mapping to stall until
  409. // the items in the queue are flushed, which makes this approach
  410. // slow on most drivers. On macOS, having a separate driver thread
  411. // is opt-in via an API, and we don't do it, so we can use this
  412. // instead of the (potentially slower) SubData approach.
  413. #ifdef LOVE_MACOSX
  414. return new StreamBufferMapSync(mode, size);
  415. #endif
  416. }
  417. return new StreamBufferSubDataOrphan(mode, size);
  418. }
  419. else
  420. return new StreamBufferClientMemory(mode, size);
  421. }
  422. } // opengl
  423. } // graphics
  424. } // love