D3D9IndexBuffer.cpp 7.7 KB

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