D3D11VertexBuffer.cpp 7.9 KB

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