D3D11IndexBuffer.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../../Precompiled.h"
  4. #include "../../Core/Context.h"
  5. #include "../../Graphics/Graphics.h"
  6. #include "../../GraphicsAPI/Direct3D11/D3D11GraphicsImpl.h"
  7. #include "../../GraphicsAPI/IndexBuffer.h"
  8. #include "../../IO/Log.h"
  9. #include "../../DebugNew.h"
  10. namespace Urho3D
  11. {
  12. void IndexBuffer::OnDeviceLost_D3D11()
  13. {
  14. // No-op on Direct3D11
  15. }
  16. void IndexBuffer::OnDeviceReset_D3D11()
  17. {
  18. // No-op on Direct3D11
  19. }
  20. void IndexBuffer::Release_D3D11()
  21. {
  22. Unlock_D3D11();
  23. if (graphics_ && graphics_->GetIndexBuffer() == this)
  24. graphics_->SetIndexBuffer(nullptr);
  25. URHO3D_SAFE_RELEASE(object_.ptr_);
  26. }
  27. bool IndexBuffer::SetData_D3D11(const void* data)
  28. {
  29. if (!data)
  30. {
  31. URHO3D_LOGERROR("Null pointer for index buffer data");
  32. return false;
  33. }
  34. if (!indexSize_)
  35. {
  36. URHO3D_LOGERROR("Index size not defined, can not set index buffer data");
  37. return false;
  38. }
  39. if (shadowData_ && data != shadowData_.Get())
  40. memcpy(shadowData_.Get(), data, (size_t)indexCount_ * indexSize_);
  41. if (object_.ptr_)
  42. {
  43. if (dynamic_)
  44. {
  45. void* hwData = MapBuffer_D3D11(0, indexCount_, true);
  46. if (hwData)
  47. {
  48. memcpy(hwData, data, (size_t)indexCount_ * indexSize_);
  49. UnmapBuffer();
  50. }
  51. else
  52. return false;
  53. }
  54. else
  55. {
  56. D3D11_BOX destBox;
  57. destBox.left = 0;
  58. destBox.right = (UINT)indexCount_ * indexSize_;
  59. destBox.top = 0;
  60. destBox.bottom = 1;
  61. destBox.front = 0;
  62. destBox.back = 1;
  63. graphics_->GetImpl_D3D11()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_.ptr_, 0, &destBox, data, 0, 0);
  64. }
  65. }
  66. return true;
  67. }
  68. bool IndexBuffer::SetDataRange_D3D11(const void* data, i32 start, i32 count, bool discard)
  69. {
  70. assert(start >= 0 && count >= 0);
  71. if (start == 0 && count == indexCount_)
  72. return SetData_D3D11(data);
  73. if (!data)
  74. {
  75. URHO3D_LOGERROR("Null pointer for index buffer data");
  76. return false;
  77. }
  78. if (!indexSize_)
  79. {
  80. URHO3D_LOGERROR("Index size not defined, can not set index buffer data");
  81. return false;
  82. }
  83. if (start + count > indexCount_)
  84. {
  85. URHO3D_LOGERROR("Illegal range for setting new index buffer data");
  86. return false;
  87. }
  88. if (!count)
  89. return true;
  90. byte* dst = shadowData_.Get() + (intptr_t)start * indexSize_;
  91. if (shadowData_ && dst != data)
  92. memcpy(dst, data, (size_t)count * indexSize_);
  93. if (object_.ptr_)
  94. {
  95. if (dynamic_)
  96. {
  97. void* hwData = MapBuffer_D3D11(start, count, discard);
  98. if (hwData)
  99. {
  100. memcpy(hwData, data, (size_t)count * indexSize_);
  101. UnmapBuffer();
  102. }
  103. else
  104. return false;
  105. }
  106. else
  107. {
  108. D3D11_BOX destBox;
  109. destBox.left = (UINT)start * indexSize_;
  110. destBox.right = destBox.left + (UINT)count * indexSize_;
  111. destBox.top = 0;
  112. destBox.bottom = 1;
  113. destBox.front = 0;
  114. destBox.back = 1;
  115. graphics_->GetImpl_D3D11()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_.ptr_, 0, &destBox, data, 0, 0);
  116. }
  117. }
  118. return true;
  119. }
  120. void* IndexBuffer::Lock_D3D11(i32 start, i32 count, bool discard)
  121. {
  122. assert(start >= 0 && count >= 0);
  123. if (lockState_ != LOCK_NONE)
  124. {
  125. URHO3D_LOGERROR("Index buffer already locked");
  126. return nullptr;
  127. }
  128. if (!indexSize_)
  129. {
  130. URHO3D_LOGERROR("Index size not defined, can not lock index buffer");
  131. return nullptr;
  132. }
  133. if (start + count > indexCount_)
  134. {
  135. URHO3D_LOGERROR("Illegal range for locking index 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_ && dynamic_)
  144. return MapBuffer_D3D11(start, count, discard);
  145. else if (shadowData_)
  146. {
  147. lockState_ = LOCK_SHADOW;
  148. return shadowData_.Get() + (intptr_t)start * indexSize_;
  149. }
  150. else if (graphics_)
  151. {
  152. lockState_ = LOCK_SCRATCH;
  153. lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
  154. return lockScratchData_;
  155. }
  156. else
  157. return nullptr;
  158. }
  159. void IndexBuffer::Unlock_D3D11()
  160. {
  161. switch (lockState_)
  162. {
  163. case LOCK_HARDWARE:
  164. UnmapBuffer();
  165. break;
  166. case LOCK_SHADOW:
  167. SetDataRange_D3D11(shadowData_.Get() + (intptr_t)lockStart_ * indexSize_, lockStart_, lockCount_);
  168. lockState_ = LOCK_NONE;
  169. break;
  170. case LOCK_SCRATCH:
  171. SetDataRange_D3D11(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 IndexBuffer::Create_D3D11()
  181. {
  182. Release_D3D11();
  183. if (!indexCount_)
  184. return true;
  185. if (graphics_)
  186. {
  187. D3D11_BUFFER_DESC bufferDesc;
  188. memset(&bufferDesc, 0, sizeof bufferDesc);
  189. bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  190. bufferDesc.CPUAccessFlags = dynamic_ ? D3D11_CPU_ACCESS_WRITE : 0;
  191. bufferDesc.Usage = dynamic_ ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  192. bufferDesc.ByteWidth = (UINT)indexCount_ * indexSize_;
  193. HRESULT hr = graphics_->GetImpl_D3D11()->GetDevice()->CreateBuffer(&bufferDesc, nullptr, (ID3D11Buffer**)&object_.ptr_);
  194. if (FAILED(hr))
  195. {
  196. URHO3D_SAFE_RELEASE(object_.ptr_);
  197. URHO3D_LOGD3DERROR("Failed to create index buffer", hr);
  198. return false;
  199. }
  200. }
  201. return true;
  202. }
  203. bool IndexBuffer::UpdateToGPU_D3D11()
  204. {
  205. if (object_.ptr_ && shadowData_)
  206. return SetData_D3D11(shadowData_.Get());
  207. else
  208. return false;
  209. }
  210. void* IndexBuffer::MapBuffer_D3D11(i32 start, i32 count, bool discard)
  211. {
  212. assert(start >= 0 && count >= 0);
  213. void* hwData = nullptr;
  214. if (object_.ptr_)
  215. {
  216. D3D11_MAPPED_SUBRESOURCE mappedData;
  217. mappedData.pData = nullptr;
  218. HRESULT hr = graphics_->GetImpl_D3D11()->GetDeviceContext()->Map((ID3D11Buffer*)object_.ptr_, 0, discard ? D3D11_MAP_WRITE_DISCARD :
  219. D3D11_MAP_WRITE, 0, &mappedData);
  220. if (FAILED(hr) || !mappedData.pData)
  221. URHO3D_LOGD3DERROR("Failed to map index buffer", hr);
  222. else
  223. {
  224. hwData = mappedData.pData;
  225. lockState_ = LOCK_HARDWARE;
  226. }
  227. }
  228. return hwData;
  229. }
  230. void IndexBuffer::UnmapBuffer_D3D11()
  231. {
  232. if (object_.ptr_ && lockState_ == LOCK_HARDWARE)
  233. {
  234. graphics_->GetImpl_D3D11()->GetDeviceContext()->Unmap((ID3D11Buffer*)object_.ptr_, 0);
  235. lockState_ = LOCK_NONE;
  236. }
  237. }
  238. } // namespace Urho3D