D3D9VertexBuffer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  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 "Graphics.h"
  25. #include "GraphicsImpl.h"
  26. #include "Log.h"
  27. #include "VertexBuffer.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. const unsigned VertexBuffer::elementSize[] =
  32. {
  33. 3 * sizeof(float), // Position
  34. 3 * sizeof(float), // Normal
  35. 4 * sizeof(unsigned char), // Color
  36. 2 * sizeof(float), // Texcoord1
  37. 2 * sizeof(float), // Texcoord2
  38. 3 * sizeof(float), // Cubetexcoord1
  39. 3 * sizeof(float), // Cubetexcoord2
  40. 4 * sizeof(float), // Tangent
  41. 4 * sizeof(float), // Blendweights
  42. 4 * sizeof(unsigned char), // Blendindices
  43. 4 * sizeof(float), // Instancematrix1
  44. 4 * sizeof(float), // Instancematrix2
  45. 4 * sizeof(float) // Instancematrix3
  46. };
  47. const String VertexBuffer::elementName[] =
  48. {
  49. "Position",
  50. "Normal",
  51. "Color",
  52. "Texcoord1",
  53. "Texcoord2",
  54. "Cubetexcoord1",
  55. "Cubetexcoord2",
  56. "Tangent"
  57. "Blendweights",
  58. "Blendindices",
  59. "Instancematrix1",
  60. "Instancematrix2",
  61. "Instancematrix3"
  62. };
  63. OBJECTTYPESTATIC(VertexBuffer);
  64. VertexBuffer::VertexBuffer(Context* context) :
  65. Object(context),
  66. GPUObject(GetSubsystem<Graphics>()),
  67. vertexCount_(0),
  68. elementMask_(0),
  69. pool_(D3DPOOL_MANAGED),
  70. usage_(0),
  71. lockState_(LOCK_NONE),
  72. lockStart_(0),
  73. lockCount_(0),
  74. lockScratchData_(0),
  75. shadowed_(false)
  76. {
  77. UpdateOffsets();
  78. // Force shadowing mode if graphics subsystem does not exist
  79. if (!graphics_)
  80. shadowed_ = true;
  81. }
  82. VertexBuffer::~VertexBuffer()
  83. {
  84. Release();
  85. }
  86. void VertexBuffer::OnDeviceLost()
  87. {
  88. if (pool_ == D3DPOOL_DEFAULT)
  89. Release();
  90. }
  91. void VertexBuffer::OnDeviceReset()
  92. {
  93. if (pool_ == D3DPOOL_DEFAULT || !object_)
  94. {
  95. Create();
  96. dataLost_ = !UpdateToGPU();
  97. }
  98. else if (dataPending_)
  99. dataLost_ = !UpdateToGPU();
  100. dataPending_ = false;
  101. }
  102. void VertexBuffer::Release()
  103. {
  104. Unlock();
  105. if (object_)
  106. {
  107. if (!graphics_)
  108. return;
  109. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  110. {
  111. if (graphics_->GetVertexBuffer(i) == this)
  112. graphics_->SetVertexBuffer(0);
  113. }
  114. ((IDirect3DVertexBuffer9*)object_)->Release();
  115. object_ = 0;
  116. }
  117. }
  118. void VertexBuffer::SetShadowed(bool enable)
  119. {
  120. // If no graphics subsystem, can not disable shadowing
  121. if (!graphics_)
  122. enable = true;
  123. if (enable != shadowed_)
  124. {
  125. if (enable && vertexSize_ && vertexCount_)
  126. shadowData_ = new unsigned char[vertexCount_ * vertexSize_];
  127. else
  128. shadowData_.Reset();
  129. shadowed_ = enable;
  130. }
  131. }
  132. bool VertexBuffer::SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic)
  133. {
  134. Unlock();
  135. if (dynamic)
  136. {
  137. pool_ = D3DPOOL_DEFAULT;
  138. usage_ = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
  139. }
  140. else
  141. {
  142. pool_ = D3DPOOL_MANAGED;
  143. usage_ = 0;
  144. }
  145. vertexCount_ = vertexCount;
  146. elementMask_ = elementMask;
  147. UpdateOffsets();
  148. if (shadowed_ && vertexCount_ && vertexSize_)
  149. shadowData_ = new unsigned char[vertexCount_ * vertexSize_];
  150. else
  151. shadowData_.Reset();
  152. return Create();
  153. }
  154. bool VertexBuffer::SetData(const void* data)
  155. {
  156. if (!data)
  157. {
  158. LOGERROR("Null pointer for vertex buffer data");
  159. return false;
  160. }
  161. if (!vertexSize_)
  162. {
  163. LOGERROR("Vertex elements not defined, can not set vertex buffer data");
  164. return false;
  165. }
  166. if (shadowData_ && data != shadowData_.Get())
  167. memcpy(shadowData_.Get(), data, vertexCount_ * vertexSize_);
  168. if (object_)
  169. {
  170. if (graphics_->IsDeviceLost())
  171. {
  172. LOGWARNING("Vertex buffer data assignment while device is lost");
  173. dataPending_ = true;
  174. return true;
  175. }
  176. void* hwData = MapBuffer(0, vertexCount_, true);
  177. if (hwData)
  178. {
  179. memcpy(hwData, data, vertexCount_ * vertexSize_);
  180. UnmapBuffer();
  181. }
  182. else
  183. return false;
  184. }
  185. dataLost_ = false;
  186. return true;
  187. }
  188. bool VertexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  189. {
  190. if (start == 0 && count == vertexCount_)
  191. return SetData(data);
  192. if (!data)
  193. {
  194. LOGERROR("Null pointer for vertex buffer data");
  195. return false;
  196. }
  197. if (!vertexSize_)
  198. {
  199. LOGERROR("Vertex elements not defined, can not set vertex buffer data");
  200. return false;
  201. }
  202. if (start + count > vertexCount_)
  203. {
  204. LOGERROR("Illegal range for setting new vertex buffer data");
  205. return false;
  206. }
  207. if (!count)
  208. return true;
  209. if (shadowData_ && shadowData_.Get() + start * vertexSize_ != data)
  210. memcpy(shadowData_.Get() + start * vertexSize_, data, count * vertexSize_);
  211. if (object_)
  212. {
  213. if (graphics_->IsDeviceLost())
  214. {
  215. LOGWARNING("Vertex buffer data assignment while device is lost");
  216. dataPending_ = true;
  217. return true;
  218. }
  219. void* hwData = MapBuffer(start, count, discard);
  220. if (hwData)
  221. {
  222. memcpy(hwData, data, count * vertexSize_);
  223. UnmapBuffer();
  224. }
  225. else
  226. return false;
  227. }
  228. return true;
  229. }
  230. void* VertexBuffer::Lock(unsigned start, unsigned count, bool discard)
  231. {
  232. if (lockState_ != LOCK_NONE)
  233. {
  234. LOGERROR("Vertex buffer already locked");
  235. return 0;
  236. }
  237. if (!vertexSize_)
  238. {
  239. LOGERROR("Vertex elements not defined, can not lock vertex buffer");
  240. return 0;
  241. }
  242. if (start + count > vertexCount_)
  243. {
  244. LOGERROR("Illegal range for locking vertex buffer");
  245. return 0;
  246. }
  247. if (!count)
  248. return 0;
  249. lockStart_ = start;
  250. lockCount_ = count;
  251. // Because shadow data must be kept in sync, can only lock hardware buffer if not shadowed
  252. if (object_ && !shadowData_ && !graphics_->IsDeviceLost())
  253. return MapBuffer(start, count, discard);
  254. else if (shadowData_)
  255. {
  256. lockState_ = LOCK_SHADOW;
  257. return shadowData_.Get() + start * vertexSize_;
  258. }
  259. else if (graphics_)
  260. {
  261. lockState_ = LOCK_SCRATCH;
  262. lockScratchData_ = graphics_->ReserveScratchBuffer(count * vertexSize_);
  263. return lockScratchData_;
  264. }
  265. else
  266. return 0;
  267. }
  268. void VertexBuffer::Unlock()
  269. {
  270. switch (lockState_)
  271. {
  272. case LOCK_HARDWARE:
  273. UnmapBuffer();
  274. break;
  275. case LOCK_SHADOW:
  276. SetDataRange(shadowData_.Get() + lockStart_ * vertexSize_, lockStart_, lockCount_);
  277. lockState_ = LOCK_NONE;
  278. break;
  279. case LOCK_SCRATCH:
  280. SetDataRange(lockScratchData_, lockStart_, lockCount_);
  281. if (graphics_)
  282. graphics_->FreeScratchBuffer(lockScratchData_);
  283. lockScratchData_ = 0;
  284. lockState_ = LOCK_NONE;
  285. break;
  286. }
  287. }
  288. bool VertexBuffer::IsDynamic() const
  289. {
  290. return pool_ == D3DPOOL_DEFAULT;
  291. }
  292. void VertexBuffer::UpdateOffsets()
  293. {
  294. unsigned elementOffset = 0;
  295. for (unsigned i = 0; i < MAX_VERTEX_ELEMENTS; ++i)
  296. {
  297. if (elementMask_ & (1 << i))
  298. {
  299. elementOffset_[i] = elementOffset;
  300. elementOffset += elementSize[i];
  301. }
  302. else
  303. elementOffset_[i] = NO_ELEMENT;
  304. }
  305. vertexSize_ = elementOffset;
  306. }
  307. unsigned long long VertexBuffer::GetBufferHash(unsigned streamIndex, unsigned useMask)
  308. {
  309. unsigned long long bufferHash = elementMask_;
  310. unsigned long long maskHash;
  311. if (useMask == MASK_DEFAULT)
  312. maskHash = ((unsigned long long)elementMask_) * 0x100000000ULL;
  313. else
  314. maskHash = ((unsigned long long)useMask) * 0x100000000ULL;
  315. bufferHash |= maskHash;
  316. bufferHash <<= streamIndex * MAX_VERTEX_ELEMENTS;
  317. return bufferHash;
  318. }
  319. unsigned VertexBuffer::GetVertexSize(unsigned elementMask)
  320. {
  321. unsigned vertexSize = 0;
  322. for (unsigned i = 0; i < MAX_VERTEX_ELEMENTS; ++i)
  323. {
  324. if (elementMask & (1 << i))
  325. vertexSize += elementSize[i];
  326. }
  327. return vertexSize;
  328. }
  329. unsigned VertexBuffer::GetElementOffset(unsigned elementMask, VertexElement element)
  330. {
  331. unsigned offset = 0;
  332. for (unsigned i = 0; i < MAX_VERTEX_ELEMENTS; ++i)
  333. {
  334. if (i == element)
  335. break;
  336. if (elementMask & (1 << i))
  337. offset += elementSize[i];
  338. }
  339. return offset;
  340. }
  341. bool VertexBuffer::Create()
  342. {
  343. Release();
  344. if (!vertexCount_ || !elementMask_)
  345. return true;
  346. if (graphics_)
  347. {
  348. if (graphics_->IsDeviceLost())
  349. {
  350. LOGWARNING("Vertex buffer creation while device is lost");
  351. return true;
  352. }
  353. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  354. if (!device || FAILED(device->CreateVertexBuffer(
  355. vertexCount_ * vertexSize_,
  356. usage_,
  357. 0,
  358. (D3DPOOL)pool_,
  359. (IDirect3DVertexBuffer9**)&object_,
  360. 0)))
  361. {
  362. LOGERROR("Could not create vertex buffer");
  363. return false;
  364. }
  365. }
  366. return true;
  367. }
  368. bool VertexBuffer::UpdateToGPU()
  369. {
  370. if (object_ && shadowData_)
  371. return SetData(shadowData_.Get());
  372. else
  373. return false;
  374. }
  375. void* VertexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
  376. {
  377. void* hwData = 0;
  378. if (object_)
  379. {
  380. DWORD flags = 0;
  381. if (discard && usage_ & D3DUSAGE_DYNAMIC)
  382. flags = D3DLOCK_DISCARD;
  383. if (FAILED(((IDirect3DVertexBuffer9*)object_)->Lock(start * vertexSize_, count * vertexSize_, &hwData, flags)))
  384. LOGERROR("Could not lock vertex buffer");
  385. else
  386. lockState_ = LOCK_HARDWARE;
  387. }
  388. return hwData;
  389. }
  390. void VertexBuffer::UnmapBuffer()
  391. {
  392. if (object_ && lockState_ == LOCK_HARDWARE)
  393. {
  394. ((IDirect3DVertexBuffer9*)object_)->Unlock();
  395. lockState_ = LOCK_NONE;
  396. }
  397. }
  398. }