OGLIndexBuffer.cpp 9.1 KB

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