OGLIndexBuffer.cpp 9.2 KB

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