D3D9VertexBuffer.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. // Dynamic buffers are in the default pool and need to be released on device loss
  33. if (dynamic_)
  34. Release();
  35. }
  36. void VertexBuffer::OnDeviceReset()
  37. {
  38. // Dynamic buffers are in the default pool and need to be recreated after device reset
  39. if (dynamic_ || !object_.ptr_)
  40. {
  41. Create();
  42. dataLost_ = !UpdateToGPU();
  43. }
  44. else if (dataPending_)
  45. dataLost_ = !UpdateToGPU();
  46. dataPending_ = false;
  47. }
  48. void VertexBuffer::Release()
  49. {
  50. Unlock();
  51. if (graphics_)
  52. {
  53. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  54. {
  55. if (graphics_->GetVertexBuffer(i) == this)
  56. graphics_->SetVertexBuffer(nullptr);
  57. }
  58. }
  59. URHO3D_SAFE_RELEASE(object_.ptr_);
  60. }
  61. bool VertexBuffer::SetData(const void* data)
  62. {
  63. if (!data)
  64. {
  65. URHO3D_LOGERROR("Null pointer for vertex buffer data");
  66. return false;
  67. }
  68. if (!vertexSize_)
  69. {
  70. URHO3D_LOGERROR("Vertex elements not defined, can not set vertex buffer data");
  71. return false;
  72. }
  73. if (shadowData_ && data != shadowData_.Get())
  74. memcpy(shadowData_.Get(), data, vertexCount_ * vertexSize_);
  75. if (object_.ptr_)
  76. {
  77. if (graphics_->IsDeviceLost())
  78. {
  79. URHO3D_LOGWARNING("Vertex buffer data assignment while device is lost");
  80. dataPending_ = true;
  81. return true;
  82. }
  83. void* hwData = MapBuffer(0, vertexCount_, true);
  84. if (hwData)
  85. {
  86. memcpy(hwData, data, vertexCount_ * vertexSize_);
  87. UnmapBuffer();
  88. }
  89. else
  90. return false;
  91. }
  92. dataLost_ = false;
  93. return true;
  94. }
  95. bool VertexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  96. {
  97. if (start == 0 && count == vertexCount_)
  98. return SetData(data);
  99. if (!data)
  100. {
  101. URHO3D_LOGERROR("Null pointer for vertex buffer data");
  102. return false;
  103. }
  104. if (!vertexSize_)
  105. {
  106. URHO3D_LOGERROR("Vertex elements not defined, can not set vertex buffer data");
  107. return false;
  108. }
  109. if (start + count > vertexCount_)
  110. {
  111. URHO3D_LOGERROR("Illegal range for setting new vertex buffer data");
  112. return false;
  113. }
  114. if (!count)
  115. return true;
  116. if (shadowData_ && shadowData_.Get() + start * vertexSize_ != data)
  117. memcpy(shadowData_.Get() + start * vertexSize_, data, count * vertexSize_);
  118. if (object_.ptr_)
  119. {
  120. if (graphics_->IsDeviceLost())
  121. {
  122. URHO3D_LOGWARNING("Vertex buffer data assignment while device is lost");
  123. dataPending_ = true;
  124. return true;
  125. }
  126. void* hwData = MapBuffer(start, count, discard);
  127. if (hwData)
  128. {
  129. memcpy(hwData, data, count * vertexSize_);
  130. UnmapBuffer();
  131. }
  132. else
  133. return false;
  134. }
  135. return true;
  136. }
  137. void* VertexBuffer::Lock(unsigned start, unsigned count, bool discard)
  138. {
  139. if (lockState_ != LOCK_NONE)
  140. {
  141. URHO3D_LOGERROR("Vertex buffer already locked");
  142. return nullptr;
  143. }
  144. if (!vertexSize_)
  145. {
  146. URHO3D_LOGERROR("Vertex elements not defined, can not lock vertex buffer");
  147. return nullptr;
  148. }
  149. if (start + count > vertexCount_)
  150. {
  151. URHO3D_LOGERROR("Illegal range for locking vertex buffer");
  152. return nullptr;
  153. }
  154. if (!count)
  155. return nullptr;
  156. lockStart_ = start;
  157. lockCount_ = count;
  158. // Because shadow data must be kept in sync, can only lock hardware buffer if not shadowed
  159. if (object_.ptr_ && !shadowData_ && !graphics_->IsDeviceLost())
  160. return MapBuffer(start, count, discard);
  161. else if (shadowData_)
  162. {
  163. lockState_ = LOCK_SHADOW;
  164. return shadowData_.Get() + start * vertexSize_;
  165. }
  166. else if (graphics_)
  167. {
  168. lockState_ = LOCK_SCRATCH;
  169. lockScratchData_ = graphics_->ReserveScratchBuffer(count * vertexSize_);
  170. return lockScratchData_;
  171. }
  172. else
  173. return nullptr;
  174. }
  175. void VertexBuffer::Unlock()
  176. {
  177. switch (lockState_)
  178. {
  179. case LOCK_HARDWARE:
  180. UnmapBuffer();
  181. break;
  182. case LOCK_SHADOW:
  183. SetDataRange(shadowData_.Get() + lockStart_ * vertexSize_, lockStart_, lockCount_);
  184. lockState_ = LOCK_NONE;
  185. break;
  186. case LOCK_SCRATCH:
  187. SetDataRange(lockScratchData_, lockStart_, lockCount_);
  188. if (graphics_)
  189. graphics_->FreeScratchBuffer(lockScratchData_);
  190. lockScratchData_ = nullptr;
  191. lockState_ = LOCK_NONE;
  192. break;
  193. default: break;
  194. }
  195. }
  196. bool VertexBuffer::Create()
  197. {
  198. Release();
  199. if (!vertexCount_ || elements_.Empty())
  200. return true;
  201. if (graphics_)
  202. {
  203. if (graphics_->IsDeviceLost())
  204. {
  205. URHO3D_LOGWARNING("Vertex buffer creation while device is lost");
  206. return true;
  207. }
  208. unsigned pool = dynamic_ ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
  209. unsigned d3dUsage = dynamic_ ? D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY : 0;
  210. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  211. HRESULT hr = device->CreateVertexBuffer(
  212. vertexCount_ * vertexSize_,
  213. d3dUsage,
  214. 0,
  215. (D3DPOOL)pool,
  216. (IDirect3DVertexBuffer9**)&object_.ptr_,
  217. nullptr);
  218. if (FAILED(hr))
  219. {
  220. URHO3D_SAFE_RELEASE(object_.ptr_);
  221. URHO3D_LOGD3DERROR("Could not create vertex buffer", hr);
  222. return false;
  223. }
  224. }
  225. return true;
  226. }
  227. bool VertexBuffer::UpdateToGPU()
  228. {
  229. if (object_.ptr_ && shadowData_)
  230. return SetData(shadowData_.Get());
  231. else
  232. return false;
  233. }
  234. void* VertexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
  235. {
  236. void* hwData = nullptr;
  237. if (object_.ptr_)
  238. {
  239. DWORD flags = 0;
  240. if (discard && dynamic_)
  241. flags = D3DLOCK_DISCARD;
  242. HRESULT hr = ((IDirect3DVertexBuffer9*)object_.ptr_)->Lock(start * vertexSize_, count * vertexSize_, &hwData, flags);
  243. if (FAILED(hr))
  244. URHO3D_LOGD3DERROR("Could not lock vertex buffer", hr);
  245. else
  246. lockState_ = LOCK_HARDWARE;
  247. }
  248. return hwData;
  249. }
  250. void VertexBuffer::UnmapBuffer()
  251. {
  252. if (object_.ptr_ && lockState_ == LOCK_HARDWARE)
  253. {
  254. ((IDirect3DVertexBuffer9*)object_.ptr_)->Unlock();
  255. lockState_ = LOCK_NONE;
  256. }
  257. }
  258. }