OGLIndexBuffer.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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::OnDeviceReset()
  55. {
  56. if (!object_)
  57. {
  58. Create();
  59. dataLost_ = !UpdateToGPU();
  60. }
  61. else if (dataPending_)
  62. dataLost_ = !UpdateToGPU();
  63. dataPending_ = false;
  64. }
  65. void IndexBuffer::Release()
  66. {
  67. Unlock();
  68. if (object_)
  69. {
  70. if (!graphics_ || graphics_->IsDeviceLost())
  71. return;
  72. if (graphics_->GetIndexBuffer() == this)
  73. graphics_->SetIndexBuffer(0);
  74. glDeleteBuffers(1, &object_);
  75. object_ = 0;
  76. }
  77. }
  78. void IndexBuffer::SetShadowed(bool enable)
  79. {
  80. // If no graphics subsystem, can not disable shadowing
  81. if (!graphics_)
  82. enable = true;
  83. if (enable != shadowed_)
  84. {
  85. if (enable && indexCount_ && indexSize_)
  86. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  87. else
  88. shadowData_.Reset();
  89. shadowed_ = enable;
  90. }
  91. }
  92. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  93. {
  94. Unlock();
  95. dynamic_ = dynamic;
  96. indexCount_ = indexCount;
  97. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  98. if (shadowed_ && indexCount_ && indexSize_)
  99. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  100. else
  101. shadowData_.Reset();
  102. return Create();
  103. }
  104. bool IndexBuffer::SetData(const void* data)
  105. {
  106. if (!data)
  107. {
  108. LOGERROR("Null pointer for index buffer data");
  109. return false;
  110. }
  111. if (!indexSize_)
  112. {
  113. LOGERROR("Index size not defined, can not set index buffer data");
  114. return false;
  115. }
  116. if (shadowData_ && data != shadowData_.Get())
  117. memcpy(shadowData_.Get(), data, indexCount_ * indexSize_);
  118. if (object_)
  119. {
  120. if (!graphics_->IsDeviceLost())
  121. {
  122. graphics_->SetIndexBuffer(0);
  123. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  124. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  125. }
  126. else
  127. {
  128. LOGWARNING("Index buffer data assignment while device is lost");
  129. dataPending_ = true;
  130. }
  131. }
  132. dataLost_ = false;
  133. return true;
  134. }
  135. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  136. {
  137. if (start == 0 && count == indexCount_)
  138. return SetData(data);
  139. if (!data)
  140. {
  141. LOGERROR("Null pointer for index buffer data");
  142. return false;
  143. }
  144. if (!indexSize_)
  145. {
  146. LOGERROR("Index size not defined, can not set index buffer data");
  147. return false;
  148. }
  149. if (start + count > indexCount_)
  150. {
  151. LOGERROR("Illegal range for setting new index buffer data");
  152. return false;
  153. }
  154. if (!count)
  155. return true;
  156. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  157. memcpy(shadowData_.Get() + start * indexSize_, data, count * indexSize_);
  158. if (object_)
  159. {
  160. if (!graphics_->IsDeviceLost())
  161. {
  162. graphics_->SetIndexBuffer(0);
  163. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object_);
  164. if (!discard || start != 0)
  165. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start * indexSize_, count * indexSize_, data);
  166. else
  167. glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  168. }
  169. else
  170. {
  171. LOGWARNING("Index buffer data assignment while device is lost");
  172. dataPending_ = true;
  173. }
  174. }
  175. return true;
  176. }
  177. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  178. {
  179. if (lockState_ != LOCK_NONE)
  180. {
  181. LOGERROR("Index buffer already locked");
  182. return 0;
  183. }
  184. if (!indexSize_)
  185. {
  186. LOGERROR("Index size not defined, can not lock index buffer");
  187. return 0;
  188. }
  189. if (start + count > indexCount_)
  190. {
  191. LOGERROR("Illegal range for locking index buffer");
  192. return 0;
  193. }
  194. if (!count)
  195. return 0;
  196. lockStart_ = start;
  197. lockCount_ = count;
  198. if (shadowData_)
  199. {
  200. lockState_ = LOCK_SHADOW;
  201. return shadowData_.Get() + start * indexSize_;
  202. }
  203. else if (graphics_)
  204. {
  205. lockState_ = LOCK_SCRATCH;
  206. lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
  207. return lockScratchData_;
  208. }
  209. else
  210. return 0;
  211. }
  212. void IndexBuffer::Unlock()
  213. {
  214. switch (lockState_)
  215. {
  216. case LOCK_SHADOW:
  217. SetDataRange(shadowData_.Get() + lockStart_ * indexSize_, lockStart_, lockCount_);
  218. lockState_ = LOCK_NONE;
  219. break;
  220. case LOCK_SCRATCH:
  221. SetDataRange(lockScratchData_, lockStart_, lockCount_);
  222. if (graphics_)
  223. graphics_->FreeScratchBuffer(lockScratchData_);
  224. lockScratchData_ = 0;
  225. lockState_ = LOCK_NONE;
  226. break;
  227. default:
  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 true;
  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. }
  304. }