OGLIndexBuffer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. 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_.RawPtr(), 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_.RawPtr());
  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. dynamic_ = dynamic;
  87. indexCount_ = indexCount;
  88. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  89. return Create();
  90. }
  91. bool IndexBuffer::SetData(const void* data)
  92. {
  93. if (!data)
  94. {
  95. LOGERROR("Null pointer for index buffer data");
  96. return false;
  97. }
  98. if (locked_)
  99. Unlock();
  100. if (object_)
  101. {
  102. graphics_->SetIndexBuffer(this);
  103. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  104. return true;
  105. }
  106. else if (fallbackData_)
  107. {
  108. memcpy(fallbackData_.RawPtr(), data, indexCount_ * indexSize_);
  109. return true;
  110. }
  111. return false;
  112. }
  113. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count)
  114. {
  115. if (!data)
  116. {
  117. LOGERROR("Null pointer for index buffer data");
  118. return false;
  119. }
  120. if (start + count > indexCount_)
  121. {
  122. LOGERROR("Illegal range for setting new index buffer data");
  123. return false;
  124. }
  125. if (locked_)
  126. Unlock();
  127. if (object_)
  128. {
  129. graphics_->SetIndexBuffer(this);
  130. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start * indexSize_, indexCount_ * indexSize_, data);
  131. return true;
  132. }
  133. else if (fallbackData_)
  134. {
  135. memcpy(fallbackData_.RawPtr() + start * indexSize_, data, indexCount_ * indexSize_);
  136. return true;
  137. }
  138. return false;
  139. }
  140. void* IndexBuffer::Lock(unsigned start, unsigned count, LockMode mode)
  141. {
  142. if (!object_ && !fallbackData_)
  143. return 0;
  144. if (locked_)
  145. {
  146. LOGERROR("Index buffer already locked");
  147. return 0;
  148. }
  149. if (!count || start + count > indexCount_)
  150. {
  151. LOGERROR("Illegal range for locking index buffer");
  152. return 0;
  153. }
  154. void* hwData = 0;
  155. GLenum glLockMode = GL_WRITE_ONLY;
  156. if (mode == LOCK_READONLY)
  157. glLockMode = GL_READ_ONLY;
  158. else if (mode == LOCK_NORMAL)
  159. glLockMode = GL_READ_WRITE;
  160. if (object_)
  161. {
  162. graphics_->SetIndexBuffer(this);
  163. // If locking the whole buffer in discard mode, create a new data store
  164. if (mode == LOCK_DISCARD && count == indexCount_)
  165. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  166. hwData = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, glLockMode);
  167. if (!hwData)
  168. return 0;
  169. hwData = (unsigned char*)hwData + start * indexSize_;
  170. }
  171. else
  172. hwData = fallbackData_.RawPtr() + start * indexSize_;
  173. locked_ = true;
  174. return hwData;
  175. }
  176. void IndexBuffer::Unlock()
  177. {
  178. if (locked_)
  179. {
  180. if (object_)
  181. {
  182. graphics_->SetIndexBuffer(this);
  183. glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
  184. }
  185. locked_ = false;
  186. }
  187. }
  188. void IndexBuffer::ClearDataLost()
  189. {
  190. }
  191. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  192. {
  193. void* data = Lock(start, count, LOCK_READONLY);
  194. if (!data)
  195. return false;
  196. minVertex = M_MAX_UNSIGNED;
  197. unsigned maxVertex = 0;
  198. if (indexSize_ == sizeof(unsigned))
  199. {
  200. unsigned* indices = (unsigned*)data;
  201. for (unsigned i = 0; i < count; ++i)
  202. {
  203. if (indices[i] < minVertex)
  204. minVertex = indices[i];
  205. if (indices[i] > maxVertex)
  206. maxVertex = indices[i];
  207. }
  208. }
  209. else
  210. {
  211. unsigned short* indices = (unsigned short*)data;
  212. for (unsigned i = 0; i < count; ++i)
  213. {
  214. if (indices[i] < minVertex)
  215. minVertex = indices[i];
  216. if (indices[i] > maxVertex)
  217. maxVertex = indices[i];
  218. }
  219. }
  220. vertexCount = maxVertex - minVertex + 1;
  221. Unlock();
  222. return true;
  223. }
  224. bool IndexBuffer::Create()
  225. {
  226. if (!indexCount_)
  227. {
  228. Release();
  229. return true;
  230. }
  231. if (graphics_)
  232. {
  233. if (!object_)
  234. glGenBuffers(1, &object_);
  235. graphics_->SetIndexBuffer(this);
  236. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  237. }
  238. else
  239. fallbackData_ = new unsigned char[indexCount_ * indexSize_];
  240. return true;
  241. }