D3D11IndexBuffer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // Copyright (c) 2008-2017 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 "../../Graphics/GraphicsImpl.h"
  26. #include "../../Graphics/IndexBuffer.h"
  27. #include "../../IO/Log.h"
  28. #include "../../DebugNew.h"
  29. namespace Urho3D
  30. {
  31. void IndexBuffer::OnDeviceLost()
  32. {
  33. // No-op on Direct3D11
  34. }
  35. void IndexBuffer::OnDeviceReset()
  36. {
  37. // No-op on Direct3D11
  38. }
  39. void IndexBuffer::Release()
  40. {
  41. Unlock();
  42. if (graphics_ && graphics_->GetIndexBuffer() == this)
  43. graphics_->SetIndexBuffer(nullptr);
  44. URHO3D_SAFE_RELEASE(object_.ptr_);
  45. }
  46. bool IndexBuffer::SetData(const void* data)
  47. {
  48. if (!data)
  49. {
  50. URHO3D_LOGERROR("Null pointer for index buffer data");
  51. return false;
  52. }
  53. if (!indexSize_)
  54. {
  55. URHO3D_LOGERROR("Index size not defined, can not set index buffer data");
  56. return false;
  57. }
  58. if (shadowData_ && data != shadowData_.Get())
  59. memcpy(shadowData_.Get(), data, indexCount_ * indexSize_);
  60. if (object_.ptr_)
  61. {
  62. if (dynamic_)
  63. {
  64. void* hwData = MapBuffer(0, indexCount_, true);
  65. if (hwData)
  66. {
  67. memcpy(hwData, data, indexCount_ * indexSize_);
  68. UnmapBuffer();
  69. }
  70. else
  71. return false;
  72. }
  73. else
  74. {
  75. D3D11_BOX destBox;
  76. destBox.left = 0;
  77. destBox.right = indexCount_ * indexSize_;
  78. destBox.top = 0;
  79. destBox.bottom = 1;
  80. destBox.front = 0;
  81. destBox.back = 1;
  82. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_.ptr_, 0, &destBox, data, 0, 0);
  83. }
  84. }
  85. return true;
  86. }
  87. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  88. {
  89. if (start == 0 && count == indexCount_)
  90. return SetData(data);
  91. if (!data)
  92. {
  93. URHO3D_LOGERROR("Null pointer for index buffer data");
  94. return false;
  95. }
  96. if (!indexSize_)
  97. {
  98. URHO3D_LOGERROR("Index size not defined, can not set index buffer data");
  99. return false;
  100. }
  101. if (start + count > indexCount_)
  102. {
  103. URHO3D_LOGERROR("Illegal range for setting new index buffer data");
  104. return false;
  105. }
  106. if (!count)
  107. return true;
  108. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  109. memcpy(shadowData_.Get() + start * indexSize_, data, count * indexSize_);
  110. if (object_.ptr_)
  111. {
  112. if (dynamic_)
  113. {
  114. void* hwData = MapBuffer(start, count, discard);
  115. if (hwData)
  116. {
  117. memcpy(hwData, data, count * indexSize_);
  118. UnmapBuffer();
  119. }
  120. else
  121. return false;
  122. }
  123. else
  124. {
  125. D3D11_BOX destBox;
  126. destBox.left = start * indexSize_;
  127. destBox.right = destBox.left + count * indexSize_;
  128. destBox.top = 0;
  129. destBox.bottom = 1;
  130. destBox.front = 0;
  131. destBox.back = 1;
  132. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_.ptr_, 0, &destBox, data, 0, 0);
  133. }
  134. }
  135. return true;
  136. }
  137. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  138. {
  139. if (lockState_ != LOCK_NONE)
  140. {
  141. URHO3D_LOGERROR("Index buffer already locked");
  142. return nullptr;
  143. }
  144. if (!indexSize_)
  145. {
  146. URHO3D_LOGERROR("Index size not defined, can not lock index buffer");
  147. return nullptr;
  148. }
  149. if (start + count > indexCount_)
  150. {
  151. URHO3D_LOGERROR("Illegal range for locking index 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_ && dynamic_)
  160. return MapBuffer(start, count, discard);
  161. else if (shadowData_)
  162. {
  163. lockState_ = LOCK_SHADOW;
  164. return shadowData_.Get() + start * indexSize_;
  165. }
  166. else if (graphics_)
  167. {
  168. lockState_ = LOCK_SCRATCH;
  169. lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
  170. return lockScratchData_;
  171. }
  172. else
  173. return nullptr;
  174. }
  175. void IndexBuffer::Unlock()
  176. {
  177. switch (lockState_)
  178. {
  179. case LOCK_HARDWARE:
  180. UnmapBuffer();
  181. break;
  182. case LOCK_SHADOW:
  183. SetDataRange(shadowData_.Get() + lockStart_ * indexSize_, 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 IndexBuffer::Create()
  197. {
  198. Release();
  199. if (!indexCount_)
  200. return true;
  201. if (graphics_)
  202. {
  203. D3D11_BUFFER_DESC bufferDesc;
  204. memset(&bufferDesc, 0, sizeof bufferDesc);
  205. bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  206. bufferDesc.CPUAccessFlags = dynamic_ ? D3D11_CPU_ACCESS_WRITE : 0;
  207. bufferDesc.Usage = dynamic_ ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  208. bufferDesc.ByteWidth = (UINT)(indexCount_ * indexSize_);
  209. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateBuffer(&bufferDesc, nullptr, (ID3D11Buffer**)&object_.ptr_);
  210. if (FAILED(hr))
  211. {
  212. URHO3D_SAFE_RELEASE(object_.ptr_);
  213. URHO3D_LOGD3DERROR("Failed to create index buffer", hr);
  214. return false;
  215. }
  216. }
  217. return true;
  218. }
  219. bool IndexBuffer::UpdateToGPU()
  220. {
  221. if (object_.ptr_ && shadowData_)
  222. return SetData(shadowData_.Get());
  223. else
  224. return false;
  225. }
  226. void* IndexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
  227. {
  228. void* hwData = nullptr;
  229. if (object_.ptr_)
  230. {
  231. D3D11_MAPPED_SUBRESOURCE mappedData;
  232. mappedData.pData = nullptr;
  233. HRESULT hr = graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Buffer*)object_.ptr_, 0, discard ? D3D11_MAP_WRITE_DISCARD :
  234. D3D11_MAP_WRITE, 0, &mappedData);
  235. if (FAILED(hr) || !mappedData.pData)
  236. URHO3D_LOGD3DERROR("Failed to map index buffer", hr);
  237. else
  238. {
  239. hwData = mappedData.pData;
  240. lockState_ = LOCK_HARDWARE;
  241. }
  242. }
  243. return hwData;
  244. }
  245. void IndexBuffer::UnmapBuffer()
  246. {
  247. if (object_.ptr_ && lockState_ == LOCK_HARDWARE)
  248. {
  249. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Buffer*)object_.ptr_, 0);
  250. lockState_ = LOCK_NONE;
  251. }
  252. }
  253. }