D3D9IndexBuffer.cpp 6.8 KB

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