OGLIndexBuffer.cpp 7.1 KB

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