OGLIndexBuffer.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "Graphics.h"
  25. #include "GraphicsImpl.h"
  26. #include "IndexBuffer.h"
  27. #include "Log.h"
  28. #include <cstring>
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. OBJECTTYPESTATIC(IndexBuffer);
  33. IndexBuffer::IndexBuffer(Context* context) :
  34. Object(context),
  35. GPUObject(GetSubsystem<Graphics>()),
  36. indexCount_(0),
  37. indexSize_(0),
  38. lockState_(LOCK_NONE),
  39. lockStart_(0),
  40. lockCount_(0),
  41. lockScratchData_(0),
  42. shadowed_(false),
  43. dynamic_(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::OnDeviceReset()
  54. {
  55. if (!object_)
  56. {
  57. Create();
  58. dataLost_ = !UpdateToGPU();
  59. }
  60. else if (dataPending_)
  61. dataLost_ = !UpdateToGPU();
  62. dataPending_ = false;
  63. }
  64. void IndexBuffer::Release()
  65. {
  66. Unlock();
  67. if (object_)
  68. {
  69. if (!graphics_ || graphics_->IsDeviceLost())
  70. return;
  71. if (graphics_->GetIndexBuffer() == this)
  72. graphics_->SetIndexBuffer(0);
  73. glDeleteBuffers(1, &object_);
  74. object_ = 0;
  75. }
  76. }
  77. void IndexBuffer::SetShadowed(bool enable)
  78. {
  79. // If no graphics subsystem, can not disable shadowing
  80. if (!graphics_)
  81. enable = true;
  82. if (enable != shadowed_)
  83. {
  84. if (enable && indexCount_ && indexSize_)
  85. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  86. else
  87. shadowData_.Reset();
  88. shadowed_ = enable;
  89. }
  90. }
  91. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  92. {
  93. Unlock();
  94. dynamic_ = dynamic;
  95. indexCount_ = indexCount;
  96. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  97. if (shadowed_ && indexCount_ && indexSize_)
  98. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  99. else
  100. shadowData_.Reset();
  101. return Create();
  102. }
  103. bool IndexBuffer::SetData(const void* data)
  104. {
  105. if (!data)
  106. {
  107. LOGERROR("Null pointer for index buffer data");
  108. return false;
  109. }
  110. if (!indexSize_)
  111. {
  112. LOGERROR("Index size not defined, can not set index buffer data");
  113. return false;
  114. }
  115. if (shadowData_ && data != shadowData_.Get())
  116. memcpy(shadowData_.Get(), data, indexCount_ * indexSize_);
  117. if (object_)
  118. {
  119. if (!graphics_->IsDeviceLost())
  120. {
  121. graphics_->SetIndexBuffer(0);
  122. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  123. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  124. }
  125. else
  126. {
  127. LOGWARNING("Index buffer data assignment while device is lost");
  128. dataPending_ = true;
  129. }
  130. }
  131. dataLost_ = false;
  132. return true;
  133. }
  134. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  135. {
  136. if (start == 0 && count == indexCount_)
  137. return SetData(data);
  138. if (!data)
  139. {
  140. LOGERROR("Null pointer for index buffer data");
  141. return false;
  142. }
  143. if (!indexSize_)
  144. {
  145. LOGERROR("Index size not defined, can not set index buffer data");
  146. return false;
  147. }
  148. if (start + count > indexCount_)
  149. {
  150. LOGERROR("Illegal range for setting new index buffer data");
  151. return false;
  152. }
  153. if (!count)
  154. return true;
  155. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  156. memcpy(shadowData_.Get() + start * indexSize_, data, count * indexSize_);
  157. if (object_)
  158. {
  159. if (!graphics_->IsDeviceLost())
  160. {
  161. graphics_->SetIndexBuffer(0);
  162. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  163. if (!discard || start != 0)
  164. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start * indexSize_, count * indexSize_, data);
  165. else
  166. glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  167. }
  168. else
  169. {
  170. LOGWARNING("Index buffer data assignment while device is lost");
  171. dataPending_ = true;
  172. }
  173. }
  174. return true;
  175. }
  176. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  177. {
  178. if (lockState_ != LOCK_NONE)
  179. {
  180. LOGERROR("Index buffer already locked");
  181. return 0;
  182. }
  183. if (!indexSize_)
  184. {
  185. LOGERROR("Index size not defined, can not lock index buffer");
  186. return 0;
  187. }
  188. if (start + count > indexCount_)
  189. {
  190. LOGERROR("Illegal range for locking index buffer");
  191. return 0;
  192. }
  193. if (!count)
  194. return 0;
  195. lockStart_ = start;
  196. lockCount_ = count;
  197. if (shadowData_)
  198. {
  199. lockState_ = LOCK_SHADOW;
  200. return shadowData_.Get() + start * indexSize_;
  201. }
  202. else if (graphics_)
  203. {
  204. lockState_ = LOCK_SCRATCH;
  205. lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
  206. return lockScratchData_;
  207. }
  208. else
  209. return 0;
  210. }
  211. void IndexBuffer::Unlock()
  212. {
  213. switch (lockState_)
  214. {
  215. case LOCK_SHADOW:
  216. SetDataRange(shadowData_.Get() + lockStart_ * indexSize_, lockStart_, lockCount_);
  217. lockState_ = LOCK_NONE;
  218. break;
  219. case LOCK_SCRATCH:
  220. SetDataRange(lockScratchData_, lockStart_, lockCount_);
  221. if (graphics_)
  222. graphics_->FreeScratchBuffer(lockScratchData_);
  223. lockScratchData_ = 0;
  224. lockState_ = LOCK_NONE;
  225. break;
  226. default:
  227. break;
  228. }
  229. }
  230. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  231. {
  232. if (!shadowData_)
  233. {
  234. LOGERROR("Used vertex range can only be queried from an index buffer with shadow data");
  235. return false;
  236. }
  237. if (start + count > indexCount_)
  238. {
  239. LOGERROR("Illegal index range for querying used vertices");
  240. return false;
  241. }
  242. minVertex = M_MAX_UNSIGNED;
  243. unsigned maxVertex = 0;
  244. if (indexSize_ == sizeof(unsigned))
  245. {
  246. unsigned* indices = ((unsigned*)shadowData_.Get()) + start;
  247. for (unsigned i = 0; i < count; ++i)
  248. {
  249. if (indices[i] < minVertex)
  250. minVertex = indices[i];
  251. if (indices[i] > maxVertex)
  252. maxVertex = indices[i];
  253. }
  254. }
  255. else
  256. {
  257. unsigned short* indices = ((unsigned short*)shadowData_.Get()) + start;
  258. for (unsigned i = 0; i < count; ++i)
  259. {
  260. if (indices[i] < minVertex)
  261. minVertex = indices[i];
  262. if (indices[i] > maxVertex)
  263. maxVertex = indices[i];
  264. }
  265. }
  266. vertexCount = maxVertex - minVertex + 1;
  267. return true;
  268. }
  269. bool IndexBuffer::Create()
  270. {
  271. if (!indexCount_)
  272. {
  273. Release();
  274. return true;
  275. }
  276. if (graphics_)
  277. {
  278. if (graphics_->IsDeviceLost())
  279. {
  280. LOGWARNING("Index buffer creation while device is lost");
  281. return true;
  282. }
  283. graphics_->SetIndexBuffer(0);
  284. if (!object_)
  285. glGenBuffers(1, &object_);
  286. if (!object_)
  287. {
  288. LOGERROR("Failed to create index buffer");
  289. return false;
  290. }
  291. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  292. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  293. }
  294. return true;
  295. }
  296. bool IndexBuffer::UpdateToGPU()
  297. {
  298. if (object_ && shadowData_)
  299. return SetData(shadowData_.Get());
  300. else
  301. return false;
  302. }
  303. }