D3D9VertexBuffer.cpp 11 KB

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