D3D11VertexBuffer.cpp 7.2 KB

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