Buffer.cpp 5.6 KB

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