D3D9IndexBuffer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Graphics.h"
  26. #include "GraphicsImpl.h"
  27. #include "IndexBuffer.h"
  28. #include "Log.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. OBJECTTYPESTATIC(IndexBuffer);
  33. IndexBuffer::IndexBuffer(Context* context) :
  34. Object(context),
  35. GPUObject(GetSubsystem<Graphics>()),
  36. indexCount_(0),
  37. indexSize_(0),
  38. pool_(D3DPOOL_MANAGED),
  39. usage_(0),
  40. lockState_(LOCK_NONE),
  41. lockStart_(0),
  42. lockCount_(0),
  43. lockScratchData_(0),
  44. shadowed_(false)
  45. {
  46. // Force shadowing mode if graphics subsystem does not exist
  47. if (!graphics_)
  48. shadowed_ = true;
  49. }
  50. IndexBuffer::~IndexBuffer()
  51. {
  52. Release();
  53. }
  54. void IndexBuffer::OnDeviceLost()
  55. {
  56. if (pool_ == D3DPOOL_DEFAULT)
  57. Release();
  58. }
  59. void IndexBuffer::OnDeviceReset()
  60. {
  61. if (pool_ == D3DPOOL_DEFAULT || !object_)
  62. {
  63. Create();
  64. dataLost_ = !UpdateToGPU();
  65. }
  66. else if (dataPending_)
  67. dataLost_ = !UpdateToGPU();
  68. dataPending_ = false;
  69. }
  70. void IndexBuffer::Release()
  71. {
  72. Unlock();
  73. if (object_)
  74. {
  75. if (!graphics_)
  76. return;
  77. if (graphics_->GetIndexBuffer() == this)
  78. graphics_->SetIndexBuffer(0);
  79. ((IDirect3DIndexBuffer9*)object_)->Release();
  80. object_ = 0;
  81. }
  82. }
  83. void IndexBuffer::SetShadowed(bool enable)
  84. {
  85. // If no graphics subsystem, can not disable shadowing
  86. if (!graphics_)
  87. enable = true;
  88. if (enable != shadowed_)
  89. {
  90. if (enable && indexCount_ && indexSize_)
  91. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  92. else
  93. shadowData_.Reset();
  94. shadowed_ = enable;
  95. }
  96. }
  97. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  98. {
  99. Unlock();
  100. if (dynamic)
  101. {
  102. pool_ = D3DPOOL_DEFAULT;
  103. usage_ = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
  104. }
  105. else
  106. {
  107. pool_ = D3DPOOL_MANAGED;
  108. usage_ = 0;
  109. }
  110. indexCount_ = indexCount;
  111. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  112. if (shadowed_ && indexCount_ && indexSize_)
  113. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  114. else
  115. shadowData_.Reset();
  116. return Create();
  117. }
  118. bool IndexBuffer::SetData(const void* data)
  119. {
  120. if (!data)
  121. {
  122. LOGERROR("Null pointer for index buffer data");
  123. return false;
  124. }
  125. if (!indexSize_)
  126. {
  127. LOGERROR("Index size not defined, can not set index buffer data");
  128. return false;
  129. }
  130. if (shadowData_ && data != shadowData_.Get())
  131. memcpy(shadowData_.Get(), data, indexCount_ * indexSize_);
  132. if (object_)
  133. {
  134. if (graphics_->IsDeviceLost())
  135. {
  136. LOGWARNING("Index buffer data assignment while device is lost");
  137. dataPending_ = true;
  138. return true;
  139. }
  140. void* hwData = MapBuffer(0, indexCount_, true);
  141. if (hwData)
  142. {
  143. memcpy(hwData, data, indexCount_ * indexSize_);
  144. UnmapBuffer();
  145. }
  146. else
  147. return false;
  148. }
  149. dataLost_ = false;
  150. return true;
  151. }
  152. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  153. {
  154. if (start == 0 && count == indexCount_)
  155. return SetData(data);
  156. if (!data)
  157. {
  158. LOGERROR("Null pointer for index buffer data");
  159. return false;
  160. }
  161. if (!indexSize_)
  162. {
  163. LOGERROR("Index size not defined, can not set index buffer data");
  164. return false;
  165. }
  166. if (start + count > indexCount_)
  167. {
  168. LOGERROR("Illegal range for setting new index buffer data");
  169. return false;
  170. }
  171. if (!count)
  172. return true;
  173. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  174. memcpy(shadowData_.Get() + start * indexSize_, data, count * indexSize_);
  175. if (object_)
  176. {
  177. if (graphics_->IsDeviceLost())
  178. {
  179. LOGWARNING("Index buffer data assignment while device is lost");
  180. dataPending_ = true;
  181. return true;
  182. }
  183. void* hwData = MapBuffer(start, count, discard);
  184. if (hwData)
  185. {
  186. memcpy(hwData, data, count * indexSize_);
  187. UnmapBuffer();
  188. }
  189. else
  190. return false;
  191. }
  192. return true;
  193. }
  194. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  195. {
  196. if (lockState_ != LOCK_NONE)
  197. {
  198. LOGERROR("Index buffer already locked");
  199. return 0;
  200. }
  201. if (!indexSize_)
  202. {
  203. LOGERROR("Index size not defined, can not lock index buffer");
  204. return 0;
  205. }
  206. if (start + count > indexCount_)
  207. {
  208. LOGERROR("Illegal range for locking index buffer");
  209. return 0;
  210. }
  211. if (!count)
  212. return 0;
  213. lockStart_ = start;
  214. lockCount_ = count;
  215. // Because shadow data must be kept in sync, can only lock hardware buffer if not shadowed
  216. if (object_ && !shadowData_ && !graphics_->IsDeviceLost())
  217. return MapBuffer(start, count, discard);
  218. else if (shadowData_)
  219. {
  220. lockState_ = LOCK_SHADOW;
  221. return shadowData_.Get() + start * indexSize_;
  222. }
  223. else if (graphics_)
  224. {
  225. lockState_ = LOCK_SCRATCH;
  226. lockScratchData_ = graphics_->ReserveScratchBuffer(count * indexSize_);
  227. return lockScratchData_;
  228. }
  229. else
  230. return 0;
  231. }
  232. void IndexBuffer::Unlock()
  233. {
  234. switch (lockState_)
  235. {
  236. case LOCK_HARDWARE:
  237. UnmapBuffer();
  238. break;
  239. case LOCK_SHADOW:
  240. SetDataRange(shadowData_.Get() + lockStart_ * indexSize_, lockStart_, lockCount_);
  241. lockState_ = LOCK_NONE;
  242. break;
  243. case LOCK_SCRATCH:
  244. SetDataRange(lockScratchData_, lockStart_, lockCount_);
  245. if (graphics_)
  246. graphics_->FreeScratchBuffer(lockScratchData_);
  247. lockScratchData_ = 0;
  248. lockState_ = LOCK_NONE;
  249. break;
  250. }
  251. }
  252. bool IndexBuffer::IsDynamic() const
  253. {
  254. return pool_ == D3DPOOL_DEFAULT;
  255. }
  256. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  257. {
  258. if (!shadowData_)
  259. {
  260. LOGERROR("Used vertex range can only be queried from an index buffer with shadow data");
  261. return false;
  262. }
  263. if (start + count > indexCount_)
  264. {
  265. LOGERROR("Illegal index range for querying used vertices");
  266. return false;
  267. }
  268. minVertex = M_MAX_UNSIGNED;
  269. unsigned maxVertex = 0;
  270. if (indexSize_ == sizeof(unsigned))
  271. {
  272. unsigned* indices = ((unsigned*)shadowData_.Get()) + start;
  273. for (unsigned i = 0; i < count; ++i)
  274. {
  275. if (indices[i] < minVertex)
  276. minVertex = indices[i];
  277. if (indices[i] > maxVertex)
  278. maxVertex = indices[i];
  279. }
  280. }
  281. else
  282. {
  283. unsigned short* indices = ((unsigned short*)shadowData_.Get()) + start;
  284. for (unsigned i = 0; i < count; ++i)
  285. {
  286. if (indices[i] < minVertex)
  287. minVertex = indices[i];
  288. if (indices[i] > maxVertex)
  289. maxVertex = indices[i];
  290. }
  291. }
  292. vertexCount = maxVertex - minVertex + 1;
  293. return true;
  294. }
  295. bool IndexBuffer::Create()
  296. {
  297. Release();
  298. if (!indexCount_)
  299. return true;
  300. if (graphics_)
  301. {
  302. if (graphics_->IsDeviceLost())
  303. {
  304. LOGWARNING("Index buffer creation while device is lost");
  305. return true;
  306. }
  307. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  308. if (!device || FAILED(device->CreateIndexBuffer(
  309. indexCount_ * indexSize_,
  310. usage_,
  311. indexSize_ == sizeof(unsigned) ? D3DFMT_INDEX32 : D3DFMT_INDEX16,
  312. (D3DPOOL)pool_,
  313. (IDirect3DIndexBuffer9**)&object_,
  314. 0)))
  315. {
  316. LOGERROR("Could not create index buffer");
  317. return false;
  318. }
  319. }
  320. return true;
  321. }
  322. bool IndexBuffer::UpdateToGPU()
  323. {
  324. if (object_ && shadowData_)
  325. return SetData(shadowData_.Get());
  326. else
  327. return false;
  328. }
  329. void* IndexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
  330. {
  331. void* hwData = 0;
  332. if (object_)
  333. {
  334. DWORD flags = 0;
  335. if (discard && usage_ & D3DUSAGE_DYNAMIC)
  336. flags = D3DLOCK_DISCARD;
  337. if (FAILED(((IDirect3DIndexBuffer9*)object_)->Lock(start * indexSize_, count * indexSize_, &hwData, flags)))
  338. LOGERROR("Could not lock index buffer");
  339. else
  340. lockState_ = LOCK_HARDWARE;
  341. }
  342. return hwData;
  343. }
  344. void IndexBuffer::UnmapBuffer()
  345. {
  346. if (object_ && lockState_ == LOCK_HARDWARE)
  347. {
  348. ((IDirect3DIndexBuffer9*)object_)->Unlock();
  349. lockState_ = LOCK_NONE;
  350. }
  351. }
  352. }