OGLIndexBuffer.cpp 9.2 KB

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