D3D9VertexBuffer.cpp 11 KB

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