OGLIndexBuffer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. #ifndef GL_ES_VERSION_2_0
  162. GLenum glLockMode = GL_WRITE_ONLY;
  163. if (mode == LOCK_READONLY)
  164. glLockMode = GL_READ_ONLY;
  165. else if (mode == LOCK_NORMAL)
  166. glLockMode = GL_READ_WRITE;
  167. #endif
  168. // In discard mode, use a CPU side buffer to avoid stalling
  169. if (mode == LOCK_DISCARD)
  170. {
  171. hwData = discardLockData_ = graphics_->ReserveDiscardLockBuffer(count * indexSize_);
  172. discardLockStart_ = start;
  173. discardLockCount_ = count;
  174. }
  175. else
  176. {
  177. #ifndef GL_ES_VERSION_2_0
  178. graphics_->SetIndexBuffer(0);
  179. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  180. hwData = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, glLockMode);
  181. if (!hwData)
  182. return 0;
  183. hwData = (unsigned char*)hwData + start * indexSize_;
  184. #else
  185. LOGERROR("Locking the index buffer without discard not supported on OpenGL ES");
  186. #endif
  187. }
  188. }
  189. else
  190. hwData = fallbackData_.Get() + start * indexSize_;
  191. locked_ = true;
  192. return hwData;
  193. }
  194. void IndexBuffer::Unlock()
  195. {
  196. if (locked_)
  197. {
  198. locked_ = false;
  199. if (object_)
  200. {
  201. if (!discardLockData_)
  202. {
  203. #ifndef GL_ES_VERSION_2_0
  204. graphics_->SetIndexBuffer(0);
  205. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  206. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  207. #endif
  208. }
  209. else
  210. {
  211. if (discardLockCount_ < indexCount_)
  212. SetDataRange(discardLockData_, discardLockStart_, discardLockCount_);
  213. else
  214. SetData(discardLockData_);
  215. graphics_->FreeDiscardLockBuffer(discardLockData_);
  216. discardLockData_ = 0;
  217. }
  218. }
  219. }
  220. }
  221. void IndexBuffer::ClearDataLost()
  222. {
  223. }
  224. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  225. {
  226. #ifdef GL_ES_VERSION_2_0
  227. return false;
  228. #endif
  229. void* data = Lock(start, count, LOCK_READONLY);
  230. if (!data)
  231. return false;
  232. minVertex = M_MAX_UNSIGNED;
  233. unsigned maxVertex = 0;
  234. if (indexSize_ == sizeof(unsigned))
  235. {
  236. unsigned* indices = (unsigned*)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. else
  246. {
  247. unsigned short* indices = (unsigned short*)data;
  248. for (unsigned i = 0; i < count; ++i)
  249. {
  250. if (indices[i] < minVertex)
  251. minVertex = indices[i];
  252. if (indices[i] > maxVertex)
  253. maxVertex = indices[i];
  254. }
  255. }
  256. vertexCount = maxVertex - minVertex + 1;
  257. Unlock();
  258. return true;
  259. }
  260. bool IndexBuffer::Create()
  261. {
  262. if (!indexCount_)
  263. {
  264. Release();
  265. return true;
  266. }
  267. if (graphics_)
  268. {
  269. graphics_->SetIndexBuffer(0);
  270. if (!object_)
  271. glGenBuffers(1, &object_);
  272. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  273. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  274. }
  275. else
  276. fallbackData_ = new unsigned char[indexCount_ * indexSize_];
  277. return true;
  278. }