D3D11IndexBuffer.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //
  2. // Copyright (c) 2008-2015 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 Atomic
  30. {
  31. IndexBuffer::IndexBuffer(Context* context) :
  32. Object(context),
  33. GPUObject(GetSubsystem<Graphics>()),
  34. indexCount_(0),
  35. indexSize_(0),
  36. lockState_(LOCK_NONE),
  37. lockStart_(0),
  38. lockCount_(0),
  39. lockScratchData_(0),
  40. dynamic_(false),
  41. shadowed_(false)
  42. {
  43. // Force shadowing mode if graphics subsystem does not exist
  44. if (!graphics_)
  45. shadowed_ = true;
  46. }
  47. IndexBuffer::~IndexBuffer()
  48. {
  49. Release();
  50. }
  51. void IndexBuffer::Release()
  52. {
  53. Unlock();
  54. if (object_)
  55. {
  56. if (!graphics_)
  57. return;
  58. if (graphics_->GetIndexBuffer() == this)
  59. graphics_->SetIndexBuffer(0);
  60. ((ID3D11Buffer*)object_)->Release();
  61. object_ = 0;
  62. }
  63. }
  64. void IndexBuffer::SetShadowed(bool enable)
  65. {
  66. // If no graphics subsystem, can not disable shadowing
  67. if (!graphics_)
  68. enable = true;
  69. if (enable != shadowed_)
  70. {
  71. if (enable && indexCount_ && indexSize_)
  72. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  73. else
  74. shadowData_.Reset();
  75. shadowed_ = enable;
  76. }
  77. }
  78. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  79. {
  80. Unlock();
  81. dynamic_ = dynamic;
  82. indexCount_ = indexCount;
  83. indexSize_ = (unsigned)(largeIndices ? sizeof(unsigned) : sizeof(unsigned short));
  84. if (shadowed_ && indexCount_ && indexSize_)
  85. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  86. else
  87. shadowData_.Reset();
  88. return Create();
  89. }
  90. bool IndexBuffer::SetData(const void* data)
  91. {
  92. if (!data)
  93. {
  94. LOGERROR("Null pointer for index buffer data");
  95. return false;
  96. }
  97. if (!indexSize_)
  98. {
  99. LOGERROR("Index size not defined, can not set index buffer data");
  100. return false;
  101. }
  102. if (shadowData_ && data != shadowData_.Get())
  103. memcpy(shadowData_.Get(), data, indexCount_ * indexSize_);
  104. if (object_)
  105. {
  106. if (dynamic_)
  107. {
  108. void* hwData = MapBuffer(0, indexCount_, true);
  109. if (hwData)
  110. {
  111. memcpy(hwData, data, indexCount_ * indexSize_);
  112. UnmapBuffer();
  113. }
  114. else
  115. return false;
  116. }
  117. else
  118. {
  119. D3D11_BOX destBox;
  120. destBox.left = 0;
  121. destBox.right = indexCount_ * indexSize_;
  122. destBox.top = 0;
  123. destBox.bottom = 1;
  124. destBox.front = 0;
  125. destBox.back = 1;
  126. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_, 0, &destBox, data, 0, 0);
  127. }
  128. }
  129. return true;
  130. }
  131. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  132. {
  133. if (start == 0 && count == indexCount_)
  134. return SetData(data);
  135. if (!data)
  136. {
  137. LOGERROR("Null pointer for index buffer data");
  138. return false;
  139. }
  140. if (!indexSize_)
  141. {
  142. LOGERROR("Index size not defined, can not set index buffer data");
  143. return false;
  144. }
  145. if (start + count > indexCount_)
  146. {
  147. LOGERROR("Illegal range for setting new index buffer data");
  148. return false;
  149. }
  150. if (!count)
  151. return true;
  152. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  153. memcpy(shadowData_.Get() + start * indexSize_, data, count * indexSize_);
  154. if (object_)
  155. {
  156. if (dynamic_)
  157. {
  158. void* hwData = MapBuffer(start, count, discard);
  159. if (hwData)
  160. {
  161. memcpy(hwData, data, count * indexSize_);
  162. UnmapBuffer();
  163. }
  164. else
  165. return false;
  166. }
  167. else
  168. {
  169. D3D11_BOX destBox;
  170. destBox.left = start * indexSize_;
  171. destBox.right = destBox.left + count * indexSize_;
  172. destBox.top = 0;
  173. destBox.bottom = 1;
  174. destBox.front = 0;
  175. destBox.back = 1;
  176. graphics_->GetImpl()->GetDeviceContext()->UpdateSubresource((ID3D11Buffer*)object_, 0, &destBox, data, 0, 0);
  177. }
  178. }
  179. return true;
  180. }
  181. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  182. {
  183. if (lockState_ != LOCK_NONE)
  184. {
  185. LOGERROR("Index buffer already locked");
  186. return 0;
  187. }
  188. if (!indexSize_)
  189. {
  190. LOGERROR("Index size not defined, can not lock index buffer");
  191. return 0;
  192. }
  193. if (start + count > indexCount_)
  194. {
  195. LOGERROR("Illegal range for locking index buffer");
  196. return 0;
  197. }
  198. if (!count)
  199. return 0;
  200. lockStart_ = start;
  201. lockCount_ = count;
  202. // Because shadow data must be kept in sync, can only lock hardware buffer if not shadowed
  203. if (object_ && !shadowData_ && dynamic_)
  204. return MapBuffer(start, count, discard);
  205. else if (shadowData_)
  206. {
  207. lockState_ = LOCK_SHADOW;
  208. return shadowData_.Get() + start * indexSize_;
  209. }
  210. else if (graphics_)
  211. {
  212. lockState_ = LOCK_SCRATCH;
  213. lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
  214. return lockScratchData_;
  215. }
  216. else
  217. return 0;
  218. }
  219. void IndexBuffer::Unlock()
  220. {
  221. switch (lockState_)
  222. {
  223. case LOCK_HARDWARE:
  224. UnmapBuffer();
  225. break;
  226. case LOCK_SHADOW:
  227. SetDataRange(shadowData_.Get() + lockStart_ * indexSize_, lockStart_, lockCount_);
  228. lockState_ = LOCK_NONE;
  229. break;
  230. case LOCK_SCRATCH:
  231. SetDataRange(lockScratchData_, lockStart_, lockCount_);
  232. if (graphics_)
  233. graphics_->FreeScratchBuffer(lockScratchData_);
  234. lockScratchData_ = 0;
  235. lockState_ = LOCK_NONE;
  236. break;
  237. default: break;
  238. }
  239. }
  240. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  241. {
  242. if (!shadowData_)
  243. {
  244. LOGERROR("Used vertex range can only be queried from an index buffer with shadow data");
  245. return false;
  246. }
  247. if (start + count > indexCount_)
  248. {
  249. LOGERROR("Illegal index range for querying used vertices");
  250. return false;
  251. }
  252. minVertex = M_MAX_UNSIGNED;
  253. unsigned maxVertex = 0;
  254. if (indexSize_ == sizeof(unsigned))
  255. {
  256. unsigned* indices = ((unsigned*)shadowData_.Get()) + start;
  257. for (unsigned i = 0; i < count; ++i)
  258. {
  259. if (indices[i] < minVertex)
  260. minVertex = indices[i];
  261. if (indices[i] > maxVertex)
  262. maxVertex = indices[i];
  263. }
  264. }
  265. else
  266. {
  267. unsigned short* indices = ((unsigned short*)shadowData_.Get()) + start;
  268. for (unsigned i = 0; i < count; ++i)
  269. {
  270. if (indices[i] < minVertex)
  271. minVertex = indices[i];
  272. if (indices[i] > maxVertex)
  273. maxVertex = indices[i];
  274. }
  275. }
  276. vertexCount = maxVertex - minVertex + 1;
  277. return true;
  278. }
  279. bool IndexBuffer::Create()
  280. {
  281. Release();
  282. if (!indexCount_)
  283. return true;
  284. if (graphics_)
  285. {
  286. D3D11_BUFFER_DESC bufferDesc;
  287. memset(&bufferDesc, 0, sizeof bufferDesc);
  288. bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  289. bufferDesc.CPUAccessFlags = dynamic_ ? D3D11_CPU_ACCESS_WRITE : 0;
  290. bufferDesc.Usage = dynamic_ ? D3D11_USAGE_DYNAMIC : D3D11_USAGE_DEFAULT;
  291. bufferDesc.ByteWidth = (UINT)(indexCount_ * indexSize_);
  292. graphics_->GetImpl()->GetDevice()->CreateBuffer(&bufferDesc, 0, (ID3D11Buffer**)&object_);
  293. if (!object_)
  294. {
  295. LOGERROR("Failed to create index buffer");
  296. return false;
  297. }
  298. }
  299. return true;
  300. }
  301. bool IndexBuffer::UpdateToGPU()
  302. {
  303. if (object_ && shadowData_)
  304. return SetData(shadowData_.Get());
  305. else
  306. return false;
  307. }
  308. void* IndexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
  309. {
  310. void* hwData = 0;
  311. if (object_)
  312. {
  313. D3D11_MAPPED_SUBRESOURCE mappedData;
  314. mappedData.pData = 0;
  315. graphics_->GetImpl()->GetDeviceContext()->Map((ID3D11Buffer*)object_, 0, discard ? D3D11_MAP_WRITE_DISCARD :
  316. D3D11_MAP_WRITE, 0, &mappedData);
  317. hwData = mappedData.pData;
  318. if (!hwData)
  319. LOGERROR("Failed to map index buffer");
  320. else
  321. lockState_ = LOCK_HARDWARE;
  322. }
  323. return hwData;
  324. }
  325. void IndexBuffer::UnmapBuffer()
  326. {
  327. if (object_ && lockState_ == LOCK_HARDWARE)
  328. {
  329. graphics_->GetImpl()->GetDeviceContext()->Unmap((ID3D11Buffer*)object_, 0);
  330. lockState_ = LOCK_NONE;
  331. }
  332. }
  333. }