OGLIndexBuffer.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "GraphicsImpl.h"
  27. #include "IndexBuffer.h"
  28. #include "Log.h"
  29. #include <cstring>
  30. #include "DebugNew.h"
  31. OBJECTTYPESTATIC(IndexBuffer);
  32. IndexBuffer::IndexBuffer(Context* context) :
  33. Object(context),
  34. GPUObject(GetSubsystem<Graphics>()),
  35. discardLockData_(0),
  36. indexCount_(0),
  37. indexSize_(0),
  38. dynamic_(false),
  39. locked_(false)
  40. {
  41. }
  42. IndexBuffer::~IndexBuffer()
  43. {
  44. Release();
  45. }
  46. void IndexBuffer::OnDeviceLost()
  47. {
  48. if (object_)
  49. {
  50. void* hwData = Lock(0, indexCount_, LOCK_READONLY);
  51. if (hwData)
  52. {
  53. saveData_ = new unsigned char[indexCount_ * indexSize_];
  54. memcpy(saveData_.Get(), hwData, indexCount_ * indexSize_);
  55. }
  56. Unlock();
  57. Release();
  58. }
  59. }
  60. void IndexBuffer::OnDeviceReset()
  61. {
  62. if (!object_)
  63. {
  64. Create();
  65. if (saveData_)
  66. {
  67. SetData(saveData_.Get());
  68. saveData_.Reset();
  69. }
  70. }
  71. }
  72. void IndexBuffer::Release()
  73. {
  74. if (object_)
  75. {
  76. if (!graphics_)
  77. return;
  78. Unlock();
  79. if (graphics_->GetIndexBuffer() == this)
  80. graphics_->SetIndexBuffer(0);
  81. glDeleteBuffers(1, &object_);
  82. object_ = 0;
  83. }
  84. fallbackData_.Reset();
  85. }
  86. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  87. {
  88. dynamic_ = dynamic;
  89. indexCount_ = indexCount;
  90. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  91. return Create();
  92. }
  93. bool IndexBuffer::SetData(const void* data)
  94. {
  95. if (!data)
  96. {
  97. LOGERROR("Null pointer for index buffer data");
  98. return false;
  99. }
  100. if (locked_)
  101. Unlock();
  102. if (object_)
  103. {
  104. graphics_->SetIndexBuffer(0);
  105. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  106. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  107. return true;
  108. }
  109. else if (fallbackData_)
  110. {
  111. memcpy(fallbackData_.Get(), data, indexCount_ * indexSize_);
  112. return true;
  113. }
  114. return false;
  115. }
  116. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count)
  117. {
  118. if (!data)
  119. {
  120. LOGERROR("Null pointer for index buffer data");
  121. return false;
  122. }
  123. if (start + count > indexCount_)
  124. {
  125. LOGERROR("Illegal range for setting new index buffer data");
  126. return false;
  127. }
  128. if (locked_)
  129. Unlock();
  130. if (object_)
  131. {
  132. graphics_->SetIndexBuffer(0);
  133. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  134. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start * indexSize_, count * indexSize_, data);
  135. return true;
  136. }
  137. else if (fallbackData_)
  138. {
  139. memcpy(fallbackData_.Get() + start * indexSize_, data, count * indexSize_);
  140. return true;
  141. }
  142. return false;
  143. }
  144. void* IndexBuffer::Lock(unsigned start, unsigned count, LockMode mode)
  145. {
  146. if (!object_ && !fallbackData_)
  147. return 0;
  148. if (locked_)
  149. {
  150. LOGERROR("Index buffer already locked");
  151. return 0;
  152. }
  153. if (!count || start + count > indexCount_)
  154. {
  155. LOGERROR("Illegal range for locking index buffer");
  156. return 0;
  157. }
  158. void* hwData = 0;
  159. if (object_)
  160. {
  161. GLenum glLockMode = GL_WRITE_ONLY;
  162. if (mode == LOCK_READONLY)
  163. glLockMode = GL_READ_ONLY;
  164. else if (mode == LOCK_NORMAL)
  165. glLockMode = GL_READ_WRITE;
  166. // In discard mode, use a CPU side buffer to avoid stalling
  167. if (mode == LOCK_DISCARD)
  168. {
  169. hwData = discardLockData_ = graphics_->ReserveDiscardLockBuffer(count * indexSize_);
  170. discardLockStart_ = start;
  171. discardLockCount_ = count;
  172. }
  173. else
  174. {
  175. graphics_->SetIndexBuffer(0);
  176. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  177. hwData = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, glLockMode);
  178. if (!hwData)
  179. return 0;
  180. hwData = (unsigned char*)hwData + start * indexSize_;
  181. }
  182. }
  183. else
  184. hwData = fallbackData_.Get() + start * indexSize_;
  185. locked_ = true;
  186. return hwData;
  187. }
  188. void IndexBuffer::Unlock()
  189. {
  190. if (locked_)
  191. {
  192. locked_ = false;
  193. if (object_)
  194. {
  195. if (!discardLockData_)
  196. {
  197. graphics_->SetIndexBuffer(0);
  198. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  199. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  200. }
  201. else
  202. {
  203. if (discardLockCount_ < indexCount_)
  204. SetDataRange(discardLockData_, discardLockStart_, discardLockCount_);
  205. else
  206. SetData(discardLockData_);
  207. graphics_->FreeDiscardLockBuffer(discardLockData_);
  208. discardLockData_ = 0;
  209. }
  210. }
  211. }
  212. }
  213. void IndexBuffer::ClearDataLost()
  214. {
  215. }
  216. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  217. {
  218. void* data = Lock(start, count, LOCK_READONLY);
  219. if (!data)
  220. return false;
  221. minVertex = M_MAX_UNSIGNED;
  222. unsigned maxVertex = 0;
  223. if (indexSize_ == sizeof(unsigned))
  224. {
  225. unsigned* indices = (unsigned*)data;
  226. for (unsigned i = 0; i < count; ++i)
  227. {
  228. if (indices[i] < minVertex)
  229. minVertex = indices[i];
  230. if (indices[i] > maxVertex)
  231. maxVertex = indices[i];
  232. }
  233. }
  234. else
  235. {
  236. unsigned short* indices = (unsigned short*)data;
  237. for (unsigned i = 0; i < count; ++i)
  238. {
  239. if (indices[i] < minVertex)
  240. minVertex = indices[i];
  241. if (indices[i] > maxVertex)
  242. maxVertex = indices[i];
  243. }
  244. }
  245. vertexCount = maxVertex - minVertex + 1;
  246. Unlock();
  247. return true;
  248. }
  249. bool IndexBuffer::Create()
  250. {
  251. if (!indexCount_)
  252. {
  253. Release();
  254. return true;
  255. }
  256. if (graphics_)
  257. {
  258. graphics_->SetIndexBuffer(0);
  259. if (!object_)
  260. glGenBuffers(1, &object_);
  261. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  262. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  263. }
  264. else
  265. fallbackData_ = new unsigned char[indexCount_ * indexSize_];
  266. return true;
  267. }