VertexBuffer.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. * Copyright (c) 2006-2012 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 "VertexBuffer.h"
  21. #include "common/Exception.h"
  22. #include "common/config.h"
  23. #include <cstdlib>
  24. #include <cstring>
  25. #include <algorithm>
  26. #include <limits>
  27. // Conflicts with std::numeric_limits<GLushort>::max() (Windows).
  28. #ifdef max
  29. # undef max
  30. #endif
  31. namespace love
  32. {
  33. namespace graphics
  34. {
  35. namespace opengl
  36. {
  37. // VertexBuffer
  38. VertexBuffer *VertexBuffer::Create(size_t size, GLenum target, GLenum usage)
  39. {
  40. try
  41. {
  42. // Try to create a VBO.
  43. return new VBO(size, target, usage);
  44. }
  45. catch(const love::Exception &)
  46. {
  47. // VBO not supported ... create regular array.
  48. return new VertexArray(size, target, usage);
  49. }
  50. }
  51. VertexBuffer::VertexBuffer(size_t size, GLenum target, GLenum usage)
  52. : size(size)
  53. , target(target)
  54. , usage(usage)
  55. {
  56. }
  57. VertexBuffer::~VertexBuffer()
  58. {
  59. }
  60. // VertexArray
  61. VertexArray::VertexArray(size_t size, GLenum target, GLenum usage)
  62. : VertexBuffer(size, target, usage)
  63. , buf(new char[size])
  64. {
  65. }
  66. VertexArray::~VertexArray()
  67. {
  68. delete [] buf;
  69. }
  70. void *VertexArray::map()
  71. {
  72. return buf;
  73. }
  74. void VertexArray::unmap()
  75. {
  76. }
  77. void VertexArray::bind()
  78. {
  79. }
  80. void VertexArray::unbind()
  81. {
  82. }
  83. void VertexArray::fill(size_t offset, size_t size, const void *data)
  84. {
  85. memcpy(buf + offset, data, size);
  86. }
  87. const void *VertexArray::getPointer(size_t offset) const
  88. {
  89. return buf + offset;
  90. }
  91. // VBO
  92. VBO::VBO(size_t size, GLenum target, GLenum usage)
  93. : VertexBuffer(size, target, usage)
  94. , vbo(0)
  95. , memory_map(0)
  96. , is_mapped(false)
  97. , is_dirty(true)
  98. {
  99. if (!(GLEE_ARB_vertex_buffer_object || GLEE_VERSION_1_5))
  100. throw love::Exception("Not supported");
  101. bool ok = load(false);
  102. if (!ok)
  103. throw love::Exception("Could not load VBO.");
  104. }
  105. VBO::~VBO()
  106. {
  107. if (vbo != 0)
  108. unload(false);
  109. if (memory_map)
  110. free(memory_map);
  111. }
  112. void *VBO::map()
  113. {
  114. if (is_mapped)
  115. return memory_map;
  116. if (!memory_map)
  117. {
  118. memory_map = malloc(getSize());
  119. if (!memory_map)
  120. throw love::Exception("Out of memory (oh the humanity!)");
  121. }
  122. if (is_dirty)
  123. glGetBufferSubDataARB(getTarget(), 0, getSize(), memory_map);
  124. is_mapped = true;
  125. is_dirty = false;
  126. return memory_map;
  127. }
  128. void VBO::unmap()
  129. {
  130. if (!is_mapped)
  131. return;
  132. // "orphan" current buffer to avoid implicit synchronisation on the gpu:
  133. // http://www.seas.upenn.edu/~pcozzi/OpenGLInsights/OpenGLInsights-AsynchronousBufferTransfers.pdf
  134. glBufferDataARB(getTarget(), getSize(), NULL, getUsage());
  135. glBufferDataARB(getTarget(), getSize(), memory_map, getUsage());
  136. is_mapped = false;
  137. }
  138. void VBO::bind()
  139. {
  140. if (!is_mapped)
  141. glBindBufferARB(getTarget(), vbo);
  142. }
  143. void VBO::unbind()
  144. {
  145. if (!is_mapped)
  146. glBindBufferARB(getTarget(), 0);
  147. }
  148. void VBO::fill(size_t offset, size_t size, const void *data)
  149. {
  150. if (is_mapped)
  151. {
  152. memcpy(static_cast<char *>(memory_map) + offset, data, size);
  153. }
  154. else
  155. {
  156. glBufferSubDataARB(getTarget(), offset, size, data);
  157. is_dirty = true;
  158. }
  159. }
  160. const void *VBO::getPointer(size_t offset) const
  161. {
  162. return reinterpret_cast<const void *>(offset);
  163. }
  164. bool VBO::loadVolatile()
  165. {
  166. return load(true);
  167. }
  168. void VBO::unloadVolatile()
  169. {
  170. unload(true);
  171. }
  172. bool VBO::load(bool restore)
  173. {
  174. glGenBuffersARB(1, &vbo);
  175. VertexBuffer::Bind bind(*this);
  176. // Copy the old buffer only if 'restore' was requested.
  177. const GLvoid *src = restore ? memory_map : 0;
  178. while (GL_NO_ERROR != glGetError())
  179. /* clear error messages */;
  180. // Note that if 'src' is '0', no data will be copied.
  181. glBufferDataARB(getTarget(), getSize(), src, getUsage());
  182. GLenum err = glGetError();
  183. return (GL_NO_ERROR == err);
  184. }
  185. void VBO::unload(bool save)
  186. {
  187. // Save data before unloading.
  188. if (save)
  189. {
  190. VertexBuffer::Bind bind(*this);
  191. GLint size;
  192. glGetBufferParameterivARB(getTarget(), GL_BUFFER_SIZE, &size);
  193. map(); // saves buffer content to memory_map.
  194. unmap();
  195. }
  196. glDeleteBuffersARB(1, &vbo);
  197. vbo = 0;
  198. }
  199. // VertexIndex
  200. size_t VertexIndex::maxSize = 0;
  201. size_t VertexIndex::elementSize = 0;
  202. std::list<size_t> VertexIndex::sizeRefs;
  203. VertexBuffer *VertexIndex::element_array = NULL;
  204. VertexIndex::VertexIndex(size_t size)
  205. : size(size)
  206. {
  207. // The upper limit is the maximum of GLuint divided by six (the number
  208. // of indices per size) and divided by the size of GLuint. This guarantees
  209. // no overflows when calculating the array size in bytes.
  210. // Memory issues will be handled by other exceptions.
  211. if (size == 0 || size > ((GLuint) -1) / 6 / sizeof(GLuint))
  212. throw love::Exception("Invalid size.");
  213. addSize(size);
  214. }
  215. VertexIndex::~VertexIndex()
  216. {
  217. removeSize(size);
  218. }
  219. size_t VertexIndex::getSize() const
  220. {
  221. return size;
  222. }
  223. size_t VertexIndex::getIndexCount(size_t elements) const
  224. {
  225. return elements * 6;
  226. }
  227. GLenum VertexIndex::getType(size_t s) const
  228. {
  229. // Calculates if unsigned short is big enough to hold all the vertex indices.
  230. static const GLenum type_table[] = {GL_UNSIGNED_SHORT, GL_UNSIGNED_INT};
  231. return type_table[s * 4 > std::numeric_limits<GLushort>::max()];
  232. // if buffer-size > max(GLushort) then GL_UNSIGNED_INT else GL_UNSIGNED_SHORT
  233. }
  234. size_t VertexIndex::getElementSize()
  235. {
  236. return elementSize;
  237. }
  238. VertexBuffer *VertexIndex::getVertexBuffer() const
  239. {
  240. return element_array;
  241. }
  242. const void *VertexIndex::getPointer(size_t offset) const
  243. {
  244. return element_array->getPointer(offset);
  245. }
  246. void VertexIndex::addSize(size_t newSize)
  247. {
  248. if (newSize <= maxSize)
  249. {
  250. // Current size is bigger. Append the size to list and sort.
  251. sizeRefs.push_back(newSize);
  252. sizeRefs.sort();
  253. return;
  254. }
  255. // Try to resize before adding it to the list because resize may throw.
  256. resize(newSize);
  257. sizeRefs.push_back(newSize);
  258. }
  259. void VertexIndex::removeSize(size_t oldSize)
  260. {
  261. // TODO: For debugging purposes, this should check if the size was actually found.
  262. sizeRefs.erase(std::find(sizeRefs.begin(), sizeRefs.end(), oldSize));
  263. if (sizeRefs.size() == 0)
  264. {
  265. resize(0);
  266. return;
  267. }
  268. if (oldSize == maxSize)
  269. {
  270. // Shrink if there's a smaller size.
  271. size_t newSize = sizeRefs.back();
  272. if (newSize < maxSize)
  273. resize(newSize);
  274. }
  275. }
  276. void VertexIndex::resize(size_t size)
  277. {
  278. if (size == 0)
  279. {
  280. delete element_array;
  281. element_array = NULL;
  282. maxSize = 0;
  283. return;
  284. }
  285. VertexBuffer *new_element_array;
  286. // Depending on the size, a switch to int and more memory is needed.
  287. GLenum target_type = getType(size);
  288. size_t elem_size = (target_type == GL_UNSIGNED_SHORT) ? sizeof(GLushort) : sizeof(GLuint);
  289. size_t array_size = elem_size * 6 * size;
  290. // Create may throw out-of-memory exceptions.
  291. // VertexIndex will propagate the exception and keep the old VertexBuffer.
  292. try
  293. {
  294. new_element_array = VertexBuffer::Create(array_size, GL_ELEMENT_ARRAY_BUFFER, GL_STATIC_DRAW);
  295. }
  296. catch (std::bad_alloc &)
  297. {
  298. throw love::Exception("Out of memory.");
  299. }
  300. // Allocation of the new VertexBuffer succeeded.
  301. // The old VertexBuffer can now be deleted.
  302. delete element_array;
  303. element_array = new_element_array;
  304. maxSize = size;
  305. elementSize = elem_size;
  306. switch (target_type)
  307. {
  308. case GL_UNSIGNED_SHORT:
  309. fill<GLushort>();
  310. break;
  311. case GL_UNSIGNED_INT:
  312. fill<GLuint>();
  313. break;
  314. }
  315. }
  316. template <typename T>
  317. void VertexIndex::fill()
  318. {
  319. VertexBuffer::Bind bind(*element_array);
  320. VertexBuffer::Mapper mapper(*element_array);
  321. T *indices = (T *) mapper.get();
  322. for (size_t i = 0; i < maxSize; ++i)
  323. {
  324. indices[i*6+0] = i * 4 + 0;
  325. indices[i*6+1] = i * 4 + 1;
  326. indices[i*6+2] = i * 4 + 2;
  327. indices[i*6+3] = i * 4 + 0;
  328. indices[i*6+4] = i * 4 + 2;
  329. indices[i*6+5] = i * 4 + 3;
  330. }
  331. }
  332. } // opengl
  333. } // graphics
  334. } // love