2
0

OGLIndexBuffer.cpp 6.9 KB

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