OGLIndexBuffer.cpp 9.2 KB

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