OGLIndexBuffer.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Graphics.h"
  26. #include "IndexBuffer.h"
  27. #include "Log.h"
  28. #include <GLee.h>
  29. #include <cstring>
  30. #include "DebugNew.h"
  31. OBJECTTYPESTATIC(IndexBuffer);
  32. IndexBuffer::IndexBuffer(Context* context) :
  33. Object(context),
  34. GPUObject(GetSubsystem<Graphics>()),
  35. indexCount_(0),
  36. indexSize_(0),
  37. dynamic_(false),
  38. locked_(false)
  39. {
  40. }
  41. IndexBuffer::~IndexBuffer()
  42. {
  43. Release();
  44. }
  45. void IndexBuffer::OnDeviceLost()
  46. {
  47. if (object_)
  48. {
  49. void* hwData = Lock(0, indexCount_, LOCK_READONLY);
  50. if (hwData)
  51. {
  52. saveData_ = new unsigned char[indexCount_ * indexSize_];
  53. memcpy(saveData_.GetPtr(), hwData, indexCount_ * indexSize_);
  54. }
  55. Unlock();
  56. Release();
  57. }
  58. }
  59. void IndexBuffer::OnDeviceReset()
  60. {
  61. if (!object_)
  62. {
  63. Create();
  64. if (saveData_)
  65. {
  66. SetData(saveData_.GetPtr());
  67. saveData_.Reset();
  68. }
  69. }
  70. }
  71. void IndexBuffer::Release()
  72. {
  73. if (object_)
  74. {
  75. if (!graphics_)
  76. return;
  77. if (graphics_->GetIndexBuffer() == this)
  78. graphics_->SetIndexBuffer(0);
  79. glDeleteBuffers(1, &object_);
  80. object_ = 0;
  81. }
  82. fallbackData_.Reset();
  83. }
  84. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  85. {
  86. return SetSize(indexCount, largeIndices ? sizeof(unsigned) : sizeof(unsigned short), dynamic);
  87. }
  88. bool IndexBuffer::SetSize(unsigned indexCount, unsigned indexSize, bool dynamic)
  89. {
  90. if ((indexSize != sizeof(unsigned)) && (indexSize != sizeof(unsigned short)))
  91. {
  92. LOGERROR("Index size not 2 or 4 bytes");
  93. return false;
  94. }
  95. dynamic_ = dynamic;
  96. indexCount_ = indexCount;
  97. indexSize_ = indexSize;
  98. return Create();
  99. }
  100. bool IndexBuffer::SetData(const void* data)
  101. {
  102. if (!data)
  103. {
  104. LOGERROR("Null pointer for index buffer data");
  105. return false;
  106. }
  107. if (locked_)
  108. Unlock();
  109. if (object_)
  110. {
  111. graphics_->SetIndexBuffer(this);
  112. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  113. return true;
  114. }
  115. else if (fallbackData_)
  116. {
  117. memcpy(fallbackData_.GetPtr(), data, indexCount_ * indexSize_);
  118. return true;
  119. }
  120. return false;
  121. }
  122. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count)
  123. {
  124. if (!data)
  125. {
  126. LOGERROR("Null pointer for index buffer data");
  127. return false;
  128. }
  129. if (start + count > indexCount_)
  130. {
  131. LOGERROR("Illegal range for setting new index buffer data");
  132. return false;
  133. }
  134. if (locked_)
  135. Unlock();
  136. if (object_)
  137. {
  138. graphics_->SetIndexBuffer(this);
  139. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start * indexSize_, indexCount_ * indexSize_, data);
  140. return true;
  141. }
  142. else if (fallbackData_)
  143. {
  144. memcpy(fallbackData_.GetPtr() + start * indexSize_, data, indexCount_ * indexSize_);
  145. return true;
  146. }
  147. return false;
  148. }
  149. void* IndexBuffer::Lock(unsigned start, unsigned count, LockMode mode)
  150. {
  151. if ((!object_) && (!fallbackData_))
  152. return 0;
  153. if (locked_)
  154. {
  155. LOGERROR("Index buffer already locked");
  156. return 0;
  157. }
  158. if ((!count) || (start + count > indexCount_))
  159. {
  160. LOGERROR("Illegal range for locking index buffer");
  161. return 0;
  162. }
  163. void* hwData = 0;
  164. GLenum glLockMode = GL_WRITE_ONLY;
  165. if (mode == LOCK_READONLY)
  166. glLockMode = GL_READ_ONLY;
  167. else if (mode == LOCK_NORMAL)
  168. glLockMode = GL_READ_WRITE;
  169. if (object_)
  170. {
  171. graphics_->SetIndexBuffer(this);
  172. // If locking the whole buffer in discard mode, create a new data store
  173. if ((mode == LOCK_DISCARD) && (count == indexCount_))
  174. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  175. hwData = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, glLockMode);
  176. if (!hwData)
  177. return 0;
  178. hwData = (unsigned char*)hwData + start * indexSize_;
  179. }
  180. else
  181. hwData = fallbackData_.GetPtr() + start * indexSize_;
  182. locked_ = true;
  183. return hwData;
  184. }
  185. void IndexBuffer::Unlock()
  186. {
  187. if (locked_)
  188. {
  189. if (object_)
  190. {
  191. graphics_->SetIndexBuffer(this);
  192. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  193. }
  194. locked_ = false;
  195. }
  196. }
  197. void IndexBuffer::ClearDataLost()
  198. {
  199. }
  200. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  201. {
  202. void* data = Lock(start, count, LOCK_READONLY);
  203. if (!data)
  204. return false;
  205. minVertex = M_MAX_UNSIGNED;
  206. unsigned maxVertex = 0;
  207. if (indexSize_ == sizeof(unsigned))
  208. {
  209. unsigned* indices = (unsigned*)data;
  210. for (unsigned i = 0; i < count; ++i)
  211. {
  212. if (indices[i] < minVertex)
  213. minVertex = indices[i];
  214. if (indices[i] > maxVertex)
  215. maxVertex = indices[i];
  216. }
  217. }
  218. else
  219. {
  220. unsigned short* indices = (unsigned short*)data;
  221. for (unsigned i = 0; i < count; ++i)
  222. {
  223. if (indices[i] < minVertex)
  224. minVertex = indices[i];
  225. if (indices[i] > maxVertex)
  226. maxVertex = indices[i];
  227. }
  228. }
  229. vertexCount = maxVertex - minVertex + 1;
  230. Unlock();
  231. return true;
  232. }
  233. bool IndexBuffer::Create()
  234. {
  235. if (!indexCount_)
  236. {
  237. Release();
  238. return true;
  239. }
  240. if (graphics_)
  241. {
  242. if (!object_)
  243. glGenBuffers(1, &object_);
  244. graphics_->SetIndexBuffer(this);
  245. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  246. }
  247. else
  248. fallbackData_ = new unsigned char[indexCount_ * indexSize_];
  249. return true;
  250. }