OGLVertexBuffer.cpp 7.2 KB

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