OGLIndexBuffer.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <cstring>
  29. #include "../../DebugNew.h"
  30. namespace Atomic
  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(this);
  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(this);
  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. if (!object_)
  284. glGenBuffers(1, &object_);
  285. if (!object_)
  286. {
  287. LOGERROR("Failed to create index buffer");
  288. return false;
  289. }
  290. graphics_->SetIndexBuffer(this);
  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. }