D3D11IndexBuffer.cpp 9.9 KB

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