OGLIndexBuffer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. indexCount_(0),
  36. indexSize_(0),
  37. lockState_(LOCK_NONE),
  38. lockStart_(0),
  39. lockCount_(0),
  40. lockScratchData_(0),
  41. shadowed_(false),
  42. dynamic_(false),
  43. dataLost_(false)
  44. {
  45. // Force shadowing mode if graphics subsystem does not exist
  46. if (!graphics_)
  47. shadowed_ = true;
  48. }
  49. IndexBuffer::~IndexBuffer()
  50. {
  51. Release();
  52. }
  53. void IndexBuffer::OnDeviceLost()
  54. {
  55. GPUObject::OnDeviceLost();
  56. }
  57. void IndexBuffer::OnDeviceReset()
  58. {
  59. if (!object_)
  60. {
  61. Create();
  62. dataLost_ = !UpdateToGPU();
  63. }
  64. }
  65. void IndexBuffer::Release()
  66. {
  67. Unlock();
  68. if (object_)
  69. {
  70. if (!graphics_)
  71. return;
  72. if (graphics_->GetIndexBuffer() == this)
  73. graphics_->SetIndexBuffer(0);
  74. glDeleteBuffers(1, &object_);
  75. object_ = 0;
  76. }
  77. }
  78. void IndexBuffer::SetShadowed(bool enable)
  79. {
  80. // If no graphics subsystem, can not disable shadowing
  81. if (!graphics_)
  82. enable = true;
  83. if (enable != shadowed_)
  84. {
  85. if (enable && indexCount_ && indexSize_)
  86. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  87. else
  88. shadowData_.Reset();
  89. shadowed_ = enable;
  90. }
  91. }
  92. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  93. {
  94. Unlock();
  95. dynamic_ = dynamic;
  96. indexCount_ = indexCount;
  97. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  98. if (shadowed_ && indexCount_ && indexSize_)
  99. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  100. else
  101. shadowData_.Reset();
  102. return Create();
  103. }
  104. bool IndexBuffer::SetData(const void* data)
  105. {
  106. if (!data)
  107. {
  108. LOGERROR("Null pointer for index buffer data");
  109. return false;
  110. }
  111. if (!indexSize_)
  112. {
  113. LOGERROR("Index size not defined, can not set index buffer data");
  114. return false;
  115. }
  116. if (shadowData_ && data != shadowData_.Get())
  117. memcpy(shadowData_.Get(), data, indexCount_ * indexSize_);
  118. if (object_)
  119. {
  120. graphics_->SetIndexBuffer(0);
  121. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  122. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  123. }
  124. dataLost_ = false;
  125. return true;
  126. }
  127. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  128. {
  129. if (start == 0 && count == indexCount_)
  130. return SetData(data);
  131. if (!data)
  132. {
  133. LOGERROR("Null pointer for index buffer data");
  134. return false;
  135. }
  136. if (!indexSize_)
  137. {
  138. LOGERROR("Index size not defined, can not set index buffer data");
  139. return false;
  140. }
  141. if (start + count > indexCount_)
  142. {
  143. LOGERROR("Illegal range for setting new index buffer data");
  144. return false;
  145. }
  146. if (!count)
  147. return true;
  148. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  149. memcpy(shadowData_.Get() + start * indexSize_, data, count * indexSize_);
  150. if (object_)
  151. {
  152. graphics_->SetIndexBuffer(0);
  153. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  154. if (!discard || start != 0)
  155. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start * indexSize_, count * indexSize_, data);
  156. else
  157. glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  158. }
  159. return true;
  160. }
  161. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  162. {
  163. if (lockState_ != LOCK_NONE)
  164. {
  165. LOGERROR("Index buffer already locked");
  166. return 0;
  167. }
  168. if (!indexSize_)
  169. {
  170. LOGERROR("Index size not defined, can not lock index buffer");
  171. return 0;
  172. }
  173. if (start + count > indexCount_)
  174. {
  175. LOGERROR("Illegal range for locking index buffer");
  176. return 0;
  177. }
  178. if (!count)
  179. return 0;
  180. lockStart_ = start;
  181. lockCount_ = count;
  182. if (shadowData_)
  183. {
  184. lockState_ = LOCK_SHADOW;
  185. return shadowData_.Get() + start * indexSize_;
  186. }
  187. else if (graphics_)
  188. {
  189. lockState_ = LOCK_SCRATCH;
  190. lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
  191. return lockScratchData_;
  192. }
  193. else
  194. return 0;
  195. }
  196. void IndexBuffer::Unlock()
  197. {
  198. switch (lockState_)
  199. {
  200. case LOCK_SHADOW:
  201. SetDataRange(shadowData_.Get() + lockStart_ * indexSize_, lockStart_, lockCount_);
  202. lockState_ = LOCK_NONE;
  203. break;
  204. case LOCK_SCRATCH:
  205. SetDataRange(lockScratchData_, lockStart_, lockCount_);
  206. if (graphics_)
  207. graphics_->FreeScratchBuffer(lockScratchData_);
  208. lockScratchData_ = 0;
  209. lockState_ = LOCK_NONE;
  210. break;
  211. }
  212. }
  213. void IndexBuffer::ClearDataLost()
  214. {
  215. dataLost_ = false;
  216. }
  217. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  218. {
  219. if (!shadowData_)
  220. {
  221. LOGERROR("Used vertex range can only be queried from an index buffer with shadow data");
  222. return false;
  223. }
  224. if (start + count > indexCount_)
  225. {
  226. LOGERROR("Illegal index range for querying used vertices");
  227. return false;
  228. }
  229. minVertex = M_MAX_UNSIGNED;
  230. unsigned maxVertex = 0;
  231. if (indexSize_ == sizeof(unsigned))
  232. {
  233. unsigned* indices = ((unsigned*)shadowData_.Get()) + start;
  234. for (unsigned i = 0; i < count; ++i)
  235. {
  236. if (indices[i] < minVertex)
  237. minVertex = indices[i];
  238. if (indices[i] > maxVertex)
  239. maxVertex = indices[i];
  240. }
  241. }
  242. else
  243. {
  244. unsigned short* indices = ((unsigned short*)shadowData_.Get()) + start;
  245. for (unsigned i = 0; i < count; ++i)
  246. {
  247. if (indices[i] < minVertex)
  248. minVertex = indices[i];
  249. if (indices[i] > maxVertex)
  250. maxVertex = indices[i];
  251. }
  252. }
  253. vertexCount = maxVertex - minVertex + 1;
  254. return true;
  255. }
  256. bool IndexBuffer::Create()
  257. {
  258. if (!indexCount_)
  259. {
  260. Release();
  261. return true;
  262. }
  263. if (graphics_)
  264. {
  265. graphics_->SetIndexBuffer(0);
  266. if (!object_)
  267. glGenBuffers(1, &object_);
  268. if (!object_)
  269. {
  270. LOGERROR("Failed to create index buffer");
  271. return false;
  272. }
  273. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  274. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  275. }
  276. return true;
  277. }
  278. bool IndexBuffer::UpdateToGPU()
  279. {
  280. if (object_ && shadowData_)
  281. return SetData(shadowData_.Get());
  282. else
  283. return false;
  284. }