Buffer.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "Buffer.h"
  21. #include "common/Exception.h"
  22. #include "graphics/vertex.h"
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <algorithm>
  26. #include <limits>
  27. namespace love
  28. {
  29. namespace graphics
  30. {
  31. namespace opengl
  32. {
  33. Buffer::Buffer(size_t size, const void *data, BufferTypeFlags typeflags, BufferUsage usage, uint32 mapflags)
  34. : love::graphics::Buffer(size, typeflags, usage, mapflags)
  35. , vbo(0)
  36. , memoryMap(nullptr)
  37. , modifiedOffset(0)
  38. , modifiedSize(0)
  39. {
  40. if (typeflags & BUFFERFLAG_VERTEX)
  41. mapType = BUFFER_VERTEX;
  42. else if (typeflags & BUFFERFLAG_INDEX)
  43. mapType = BUFFER_INDEX;
  44. else if (mapflags & BUFFERFLAG_UNIFORM)
  45. mapType = BUFFER_UNIFORM;
  46. else if (mapflags & BUFFERFLAG_SHADER_STORAGE)
  47. mapType = BUFFER_SHADER_STORAGE;
  48. target = OpenGL::getGLBufferType(mapType);
  49. try
  50. {
  51. memoryMap = new char[size];
  52. }
  53. catch (std::bad_alloc &)
  54. {
  55. throw love::Exception("Out of memory.");
  56. }
  57. if (data != nullptr)
  58. memcpy(memoryMap, data, size);
  59. if (!load(data != nullptr))
  60. {
  61. delete[] memoryMap;
  62. throw love::Exception("Could not load vertex buffer (out of VRAM?)");
  63. }
  64. }
  65. Buffer::~Buffer()
  66. {
  67. if (vbo != 0)
  68. unload();
  69. delete[] memoryMap;
  70. }
  71. void *Buffer::map()
  72. {
  73. if (mapped)
  74. return memoryMap;
  75. mapped = true;
  76. modifiedOffset = 0;
  77. modifiedSize = 0;
  78. return memoryMap;
  79. }
  80. void Buffer::unmapStatic(size_t offset, size_t size)
  81. {
  82. if (size == 0)
  83. return;
  84. // Upload the mapped data to the buffer.
  85. gl.bindBuffer(mapType, vbo);
  86. glBufferSubData(target, (GLintptr) offset, (GLsizeiptr) size, memoryMap + offset);
  87. }
  88. void Buffer::unmapStream()
  89. {
  90. GLenum glusage = OpenGL::getGLBufferUsage(getUsage());
  91. // "orphan" current buffer to avoid implicit synchronisation on the GPU:
  92. // http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-AsynchronousBufferTransfers.pdf
  93. gl.bindBuffer(mapType, vbo);
  94. glBufferData(target, (GLsizeiptr) getSize(), nullptr, glusage);
  95. #if LOVE_WINDOWS
  96. // TODO: Verify that this codepath is a useful optimization.
  97. if (gl.getVendor() == OpenGL::VENDOR_INTEL)
  98. glBufferData(target, (GLsizeiptr) getSize(), memoryMap, glusage);
  99. else
  100. #endif
  101. glBufferSubData(target, 0, (GLsizeiptr) getSize(), memoryMap);
  102. }
  103. void Buffer::unmap()
  104. {
  105. if (!mapped)
  106. return;
  107. if ((mapFlags & MAP_EXPLICIT_RANGE_MODIFY) != 0)
  108. {
  109. modifiedOffset = std::min(modifiedOffset, getSize() - 1);
  110. modifiedSize = std::min(modifiedSize, getSize() - modifiedOffset);
  111. }
  112. else
  113. {
  114. modifiedOffset = 0;
  115. modifiedSize = getSize();
  116. }
  117. if (modifiedSize > 0)
  118. {
  119. switch (getUsage())
  120. {
  121. case BUFFERUSAGE_STATIC:
  122. unmapStatic(modifiedOffset, modifiedSize);
  123. break;
  124. case BUFFERUSAGE_STREAM:
  125. unmapStream();
  126. break;
  127. case BUFFERUSAGE_DYNAMIC:
  128. default:
  129. // It's probably more efficient to treat it like a streaming buffer if
  130. // at least a third of its contents have been modified during the map().
  131. if (modifiedSize >= getSize() / 3)
  132. unmapStream();
  133. else
  134. unmapStatic(modifiedOffset, modifiedSize);
  135. break;
  136. }
  137. }
  138. modifiedOffset = 0;
  139. modifiedSize = 0;
  140. mapped = false;
  141. }
  142. void Buffer::setMappedRangeModified(size_t offset, size_t modifiedsize)
  143. {
  144. if (!mapped || !(mapFlags & MAP_EXPLICIT_RANGE_MODIFY))
  145. return;
  146. // We're being conservative right now by internally marking the whole range
  147. // from the start of section a to the end of section b as modified if both
  148. // a and b are marked as modified.
  149. size_t oldrangeend = modifiedOffset + modifiedSize;
  150. modifiedOffset = std::min(modifiedOffset, offset);
  151. size_t newrangeend = std::max(offset + modifiedsize, oldrangeend);
  152. modifiedSize = newrangeend - modifiedOffset;
  153. }
  154. void Buffer::fill(size_t offset, size_t size, const void *data)
  155. {
  156. memcpy(memoryMap + offset, data, size);
  157. if (mapped)
  158. setMappedRangeModified(offset, size);
  159. else
  160. {
  161. gl.bindBuffer(mapType, vbo);
  162. glBufferSubData(target, (GLintptr) offset, (GLsizeiptr) size, data);
  163. }
  164. }
  165. ptrdiff_t Buffer::getHandle() const
  166. {
  167. return vbo;
  168. }
  169. void Buffer::copyTo(size_t offset, size_t size, love::graphics::Buffer *other, size_t otheroffset)
  170. {
  171. other->fill(otheroffset, size, memoryMap + offset);
  172. }
  173. bool Buffer::loadVolatile()
  174. {
  175. return load(true);
  176. }
  177. void Buffer::unloadVolatile()
  178. {
  179. unload();
  180. }
  181. bool Buffer::load(bool restore)
  182. {
  183. glGenBuffers(1, &vbo);
  184. gl.bindBuffer(mapType, vbo);
  185. while (glGetError() != GL_NO_ERROR)
  186. /* Clear the error buffer. */;
  187. // Copy the old buffer only if 'restore' was requested.
  188. const GLvoid *src = restore ? memoryMap : nullptr;
  189. // Note that if 'src' is '0', no data will be copied.
  190. glBufferData(target, (GLsizeiptr) getSize(), src, OpenGL::getGLBufferUsage(getUsage()));
  191. return (glGetError() == GL_NO_ERROR);
  192. }
  193. void Buffer::unload()
  194. {
  195. mapped = false;
  196. gl.deleteBuffer(vbo);
  197. vbo = 0;
  198. }
  199. } // opengl
  200. } // graphics
  201. } // love