D3D9VertexBuffer.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../../Precompiled.h"
  4. #include "../../Graphics/Graphics.h"
  5. #include "../../GraphicsAPI/Direct3D9/D3D9GraphicsImpl.h"
  6. #include "../../GraphicsAPI/VertexBuffer.h"
  7. #include "../../IO/Log.h"
  8. #include "../../DebugNew.h"
  9. namespace Urho3D
  10. {
  11. void VertexBuffer::OnDeviceLost_D3D9()
  12. {
  13. // Dynamic buffers are in the default pool and need to be released on device loss
  14. if (dynamic_)
  15. Release_D3D9();
  16. }
  17. void VertexBuffer::OnDeviceReset_D3D9()
  18. {
  19. // Dynamic buffers are in the default pool and need to be recreated after device reset
  20. if (dynamic_ || !object_.ptr_)
  21. {
  22. Create_D3D9();
  23. dataLost_ = !UpdateToGPU_D3D9();
  24. }
  25. else if (dataPending_)
  26. dataLost_ = !UpdateToGPU_D3D9();
  27. dataPending_ = false;
  28. }
  29. void VertexBuffer::Release_D3D9()
  30. {
  31. Unlock_D3D9();
  32. if (graphics_)
  33. {
  34. for (i32 i = 0; i < MAX_VERTEX_STREAMS; ++i)
  35. {
  36. if (graphics_->GetVertexBuffer(i) == this)
  37. graphics_->SetVertexBuffer(nullptr);
  38. }
  39. }
  40. URHO3D_SAFE_RELEASE(object_.ptr_);
  41. }
  42. bool VertexBuffer::SetData_D3D9(const void* data)
  43. {
  44. if (!data)
  45. {
  46. URHO3D_LOGERROR("Null pointer for vertex buffer data");
  47. return false;
  48. }
  49. if (!vertexSize_)
  50. {
  51. URHO3D_LOGERROR("Vertex elements not defined, can not set vertex buffer data");
  52. return false;
  53. }
  54. if (shadowData_ && data != shadowData_.Get())
  55. memcpy(shadowData_.Get(), data, (size_t)vertexCount_ * vertexSize_);
  56. if (object_.ptr_)
  57. {
  58. if (graphics_->IsDeviceLost())
  59. {
  60. URHO3D_LOGWARNING("Vertex buffer data assignment while device is lost");
  61. dataPending_ = true;
  62. return true;
  63. }
  64. void* hwData = MapBuffer_D3D9(0, vertexCount_, true);
  65. if (hwData)
  66. {
  67. memcpy(hwData, data, (size_t)vertexCount_ * vertexSize_);
  68. UnmapBuffer_D3D9();
  69. }
  70. else
  71. return false;
  72. }
  73. dataLost_ = false;
  74. return true;
  75. }
  76. bool VertexBuffer::SetDataRange_D3D9(const void* data, i32 start, i32 count, bool discard)
  77. {
  78. assert(start >= 0 && count >= 0);
  79. if (start == 0 && count == vertexCount_)
  80. return SetData_D3D9(data);
  81. if (!data)
  82. {
  83. URHO3D_LOGERROR("Null pointer for vertex buffer data");
  84. return false;
  85. }
  86. if (!vertexSize_)
  87. {
  88. URHO3D_LOGERROR("Vertex elements not defined, can not set vertex buffer data");
  89. return false;
  90. }
  91. if (start + count > vertexCount_)
  92. {
  93. URHO3D_LOGERROR("Illegal range for setting new vertex buffer data");
  94. return false;
  95. }
  96. if (!count)
  97. return true;
  98. u8* dst = shadowData_.Get() + (intptr_t)start * vertexSize_;
  99. if (shadowData_ && dst != data)
  100. memcpy(dst, data, (size_t)count * vertexSize_);
  101. if (object_.ptr_)
  102. {
  103. if (graphics_->IsDeviceLost())
  104. {
  105. URHO3D_LOGWARNING("Vertex buffer data assignment while device is lost");
  106. dataPending_ = true;
  107. return true;
  108. }
  109. void* hwData = MapBuffer_D3D9(start, count, discard);
  110. if (hwData)
  111. {
  112. memcpy(hwData, data, (size_t)count * vertexSize_);
  113. UnmapBuffer_D3D9();
  114. }
  115. else
  116. return false;
  117. }
  118. return true;
  119. }
  120. void* VertexBuffer::Lock_D3D9(i32 start, i32 count, bool discard)
  121. {
  122. assert(start >= 0 && count >= 0);
  123. if (lockState_ != LOCK_NONE)
  124. {
  125. URHO3D_LOGERROR("Vertex buffer already locked");
  126. return nullptr;
  127. }
  128. if (!vertexSize_)
  129. {
  130. URHO3D_LOGERROR("Vertex elements not defined, can not lock vertex buffer");
  131. return nullptr;
  132. }
  133. if (start + count > vertexCount_)
  134. {
  135. URHO3D_LOGERROR("Illegal range for locking vertex buffer");
  136. return nullptr;
  137. }
  138. if (!count)
  139. return nullptr;
  140. lockStart_ = start;
  141. lockCount_ = count;
  142. // Because shadow data must be kept in sync, can only lock hardware buffer if not shadowed
  143. if (object_.ptr_ && !shadowData_ && !graphics_->IsDeviceLost())
  144. return MapBuffer_D3D9(start, count, discard);
  145. else if (shadowData_)
  146. {
  147. lockState_ = LOCK_SHADOW;
  148. return shadowData_.Get() + (intptr_t)start * vertexSize_;
  149. }
  150. else if (graphics_)
  151. {
  152. lockState_ = LOCK_SCRATCH;
  153. lockScratchData_ = graphics_->ReserveScratchBuffer(count * vertexSize_);
  154. return lockScratchData_;
  155. }
  156. else
  157. return nullptr;
  158. }
  159. void VertexBuffer::Unlock_D3D9()
  160. {
  161. switch (lockState_)
  162. {
  163. case LOCK_HARDWARE:
  164. UnmapBuffer_D3D9();
  165. break;
  166. case LOCK_SHADOW:
  167. SetDataRange_D3D9(shadowData_.Get() + (intptr_t)lockStart_ * vertexSize_, lockStart_, lockCount_);
  168. lockState_ = LOCK_NONE;
  169. break;
  170. case LOCK_SCRATCH:
  171. SetDataRange_D3D9(lockScratchData_, lockStart_, lockCount_);
  172. if (graphics_)
  173. graphics_->FreeScratchBuffer(lockScratchData_);
  174. lockScratchData_ = nullptr;
  175. lockState_ = LOCK_NONE;
  176. break;
  177. default: break;
  178. }
  179. }
  180. bool VertexBuffer::Create_D3D9()
  181. {
  182. Release_D3D9();
  183. if (!vertexCount_ || elements_.Empty())
  184. return true;
  185. if (graphics_)
  186. {
  187. if (graphics_->IsDeviceLost())
  188. {
  189. URHO3D_LOGWARNING("Vertex buffer creation while device is lost");
  190. return true;
  191. }
  192. D3DPOOL pool = dynamic_ ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
  193. DWORD d3dUsage = dynamic_ ? D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY : 0;
  194. IDirect3DDevice9* device = graphics_->GetImpl_D3D9()->GetDevice();
  195. HRESULT hr = device->CreateVertexBuffer(
  196. (UINT)vertexCount_ * vertexSize_,
  197. d3dUsage,
  198. 0,
  199. pool,
  200. (IDirect3DVertexBuffer9**)&object_.ptr_,
  201. nullptr);
  202. if (FAILED(hr))
  203. {
  204. URHO3D_SAFE_RELEASE(object_.ptr_);
  205. URHO3D_LOGD3DERROR("Could not create vertex buffer", hr);
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. bool VertexBuffer::UpdateToGPU_D3D9()
  212. {
  213. if (object_.ptr_ && shadowData_)
  214. return SetData_D3D9(shadowData_.Get());
  215. else
  216. return false;
  217. }
  218. void* VertexBuffer::MapBuffer_D3D9(i32 start, i32 count, bool discard)
  219. {
  220. assert(start >= 0 && count >= 0);
  221. void* hwData = nullptr;
  222. if (object_.ptr_)
  223. {
  224. DWORD flags = 0;
  225. if (discard && dynamic_)
  226. flags = D3DLOCK_DISCARD;
  227. HRESULT hr = ((IDirect3DVertexBuffer9*)object_.ptr_)->Lock((UINT)start * vertexSize_, (UINT)count * vertexSize_, &hwData, flags);
  228. if (FAILED(hr))
  229. URHO3D_LOGD3DERROR("Could not lock vertex buffer", hr);
  230. else
  231. lockState_ = LOCK_HARDWARE;
  232. }
  233. return hwData;
  234. }
  235. void VertexBuffer::UnmapBuffer_D3D9()
  236. {
  237. if (object_.ptr_ && lockState_ == LOCK_HARDWARE)
  238. {
  239. ((IDirect3DVertexBuffer9*)object_.ptr_)->Unlock();
  240. lockState_ = LOCK_NONE;
  241. }
  242. }
  243. }