OGLIndexBuffer.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // Copyright (c) 2008-2015 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 "../../Core/Context.h"
  24. #include "../../Graphics/Graphics.h"
  25. #include "../../Graphics/GraphicsImpl.h"
  26. #include "../../Graphics/IndexBuffer.h"
  27. #include "../../IO/Log.h"
  28. #include "../../DebugNew.h"
  29. namespace Atomic
  30. {
  31. IndexBuffer::IndexBuffer(Context* context) :
  32. Object(context),
  33. GPUObject(GetSubsystem<Graphics>()),
  34. indexCount_(0),
  35. indexSize_(0),
  36. lockState_(LOCK_NONE),
  37. lockStart_(0),
  38. lockCount_(0),
  39. lockScratchData_(0),
  40. shadowed_(false),
  41. dynamic_(false)
  42. {
  43. // Force shadowing mode if graphics subsystem does not exist
  44. if (!graphics_)
  45. shadowed_ = true;
  46. }
  47. IndexBuffer::~IndexBuffer()
  48. {
  49. Release();
  50. }
  51. void IndexBuffer::OnDeviceReset()
  52. {
  53. if (!object_)
  54. {
  55. Create();
  56. dataLost_ = !UpdateToGPU();
  57. }
  58. else if (dataPending_)
  59. dataLost_ = !UpdateToGPU();
  60. dataPending_ = false;
  61. }
  62. void IndexBuffer::Release()
  63. {
  64. Unlock();
  65. if (object_)
  66. {
  67. if (!graphics_)
  68. return;
  69. if (!graphics_->IsDeviceLost())
  70. {
  71. if (graphics_->GetIndexBuffer() == this)
  72. graphics_->SetIndexBuffer(0);
  73. glDeleteBuffers(1, &object_);
  74. }
  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_ = (unsigned)(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(this);
  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(this);
  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. if (!object_)
  283. glGenBuffers(1, &object_);
  284. if (!object_)
  285. {
  286. LOGERROR("Failed to create index buffer");
  287. return false;
  288. }
  289. graphics_->SetIndexBuffer(this);
  290. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * indexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  291. }
  292. return true;
  293. }
  294. bool IndexBuffer::UpdateToGPU()
  295. {
  296. if (object_ && shadowData_)
  297. return SetData(shadowData_.Get());
  298. else
  299. return false;
  300. }
  301. }