OGLIndexBuffer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // Copyright (c) 2008-2020 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 Urho3D
  30. {
  31. void IndexBuffer::OnDeviceLost()
  32. {
  33. if (object_.name_ && !graphics_->IsDeviceLost())
  34. glDeleteBuffers(1, &object_.name_);
  35. GPUObject::OnDeviceLost();
  36. }
  37. void IndexBuffer::OnDeviceReset()
  38. {
  39. if (!object_.name_)
  40. {
  41. Create();
  42. dataLost_ = !UpdateToGPU();
  43. }
  44. else if (dataPending_)
  45. dataLost_ = !UpdateToGPU();
  46. dataPending_ = false;
  47. }
  48. void IndexBuffer::Release()
  49. {
  50. Unlock();
  51. if (object_.name_)
  52. {
  53. if (!graphics_)
  54. return;
  55. if (!graphics_->IsDeviceLost())
  56. {
  57. if (graphics_->GetIndexBuffer() == this)
  58. graphics_->SetIndexBuffer(nullptr);
  59. glDeleteBuffers(1, &object_.name_);
  60. }
  61. object_.name_ = 0;
  62. }
  63. }
  64. bool IndexBuffer::SetData(const void* data)
  65. {
  66. if (!data)
  67. {
  68. URHO3D_LOGERROR("Null pointer for index buffer data");
  69. return false;
  70. }
  71. if (!indexSize_)
  72. {
  73. URHO3D_LOGERROR("Index size not defined, can not set index buffer data");
  74. return false;
  75. }
  76. if (shadowData_ && data != shadowData_.Get())
  77. memcpy(shadowData_.Get(), data, indexCount_ * (size_t)indexSize_);
  78. if (object_.name_)
  79. {
  80. if (!graphics_->IsDeviceLost())
  81. {
  82. graphics_->SetIndexBuffer(this);
  83. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * (size_t)indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  84. }
  85. else
  86. {
  87. URHO3D_LOGWARNING("Index buffer data assignment while device is lost");
  88. dataPending_ = true;
  89. }
  90. }
  91. dataLost_ = false;
  92. return true;
  93. }
  94. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  95. {
  96. if (start == 0 && count == indexCount_)
  97. return SetData(data);
  98. if (!data)
  99. {
  100. URHO3D_LOGERROR("Null pointer for index buffer data");
  101. return false;
  102. }
  103. if (!indexSize_)
  104. {
  105. URHO3D_LOGERROR("Index size not defined, can not set index buffer data");
  106. return false;
  107. }
  108. if (start + count > indexCount_)
  109. {
  110. URHO3D_LOGERROR("Illegal range for setting new index buffer data");
  111. return false;
  112. }
  113. if (!count)
  114. return true;
  115. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  116. memcpy(shadowData_.Get() + start * indexSize_, data, count * (size_t)indexSize_);
  117. if (object_.name_)
  118. {
  119. if (!graphics_->IsDeviceLost())
  120. {
  121. graphics_->SetIndexBuffer(this);
  122. if (!discard || start != 0)
  123. glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start * (size_t)indexSize_, count * indexSize_, data);
  124. else
  125. glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * (size_t)indexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  126. }
  127. else
  128. {
  129. URHO3D_LOGWARNING("Index buffer data assignment while device is lost");
  130. dataPending_ = true;
  131. }
  132. }
  133. return true;
  134. }
  135. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  136. {
  137. if (lockState_ != LOCK_NONE)
  138. {
  139. URHO3D_LOGERROR("Index buffer already locked");
  140. return nullptr;
  141. }
  142. if (!indexSize_)
  143. {
  144. URHO3D_LOGERROR("Index size not defined, can not lock index buffer");
  145. return nullptr;
  146. }
  147. if (start + count > indexCount_)
  148. {
  149. URHO3D_LOGERROR("Illegal range for locking index buffer");
  150. return nullptr;
  151. }
  152. if (!count)
  153. return nullptr;
  154. lockStart_ = start;
  155. lockCount_ = count;
  156. discardLock_ = discard;
  157. if (shadowData_)
  158. {
  159. lockState_ = LOCK_SHADOW;
  160. return shadowData_.Get() + start * indexSize_;
  161. }
  162. else if (graphics_)
  163. {
  164. lockState_ = LOCK_SCRATCH;
  165. lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
  166. return lockScratchData_;
  167. }
  168. else
  169. return nullptr;
  170. }
  171. void IndexBuffer::Unlock()
  172. {
  173. switch (lockState_)
  174. {
  175. case LOCK_SHADOW:
  176. SetDataRange(shadowData_.Get() + lockStart_ * indexSize_, lockStart_, lockCount_, discardLock_);
  177. lockState_ = LOCK_NONE;
  178. break;
  179. case LOCK_SCRATCH:
  180. SetDataRange(lockScratchData_, lockStart_, lockCount_, discardLock_);
  181. if (graphics_)
  182. graphics_->FreeScratchBuffer(lockScratchData_);
  183. lockScratchData_ = nullptr;
  184. lockState_ = LOCK_NONE;
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. bool IndexBuffer::Create()
  191. {
  192. if (!indexCount_)
  193. {
  194. Release();
  195. return true;
  196. }
  197. if (graphics_)
  198. {
  199. if (graphics_->IsDeviceLost())
  200. {
  201. URHO3D_LOGWARNING("Index buffer creation while device is lost");
  202. return true;
  203. }
  204. if (!object_.name_)
  205. glGenBuffers(1, &object_.name_);
  206. if (!object_.name_)
  207. {
  208. URHO3D_LOGERROR("Failed to create index buffer");
  209. return false;
  210. }
  211. graphics_->SetIndexBuffer(this);
  212. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount_ * (size_t)indexSize_, nullptr, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  213. }
  214. return true;
  215. }
  216. bool IndexBuffer::UpdateToGPU()
  217. {
  218. if (object_.name_ && shadowData_)
  219. return SetData(shadowData_.Get());
  220. else
  221. return false;
  222. }
  223. void* IndexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
  224. {
  225. // Never called on OpenGL
  226. return nullptr;
  227. }
  228. void IndexBuffer::UnmapBuffer()
  229. {
  230. // Never called on OpenGL
  231. }
  232. }