D3D9IndexBuffer.cpp 10 KB

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