OGLVertexBuffer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 <cstring>
  29. #include "DebugNew.h"
  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. const unsigned VertexBuffer::elementType[] =
  47. {
  48. GL_FLOAT, // Position
  49. GL_FLOAT, // Normal
  50. GL_UNSIGNED_BYTE, // Color
  51. GL_FLOAT, // Texcoord1
  52. GL_FLOAT, // Texcoord2
  53. GL_FLOAT, // Cubetexcoord1
  54. GL_FLOAT, // Cubetexcoord2
  55. GL_FLOAT, // Tangent
  56. GL_FLOAT, // Blendweights
  57. GL_UNSIGNED_BYTE, // Blendindices
  58. GL_FLOAT, // Instancematrix1
  59. GL_FLOAT, // Instancematrix2
  60. GL_FLOAT // Instancematrix3
  61. };
  62. const unsigned VertexBuffer::elementComponents[] =
  63. {
  64. 3, // Position
  65. 3, // Normal
  66. 4, // Color
  67. 2, // Texcoord1
  68. 2, // Texcoord2
  69. 3, // Cubetexcoord1
  70. 3, // Cubetexcoord2
  71. 4, // Tangent
  72. 4, // Blendweights
  73. 4, // Blendindices
  74. 4, // Instancematrix1
  75. 4, // Instancematrix2
  76. 4 // Instancematrix3
  77. };
  78. const unsigned VertexBuffer::elementNormalize[] =
  79. {
  80. GL_FALSE, // Position
  81. GL_FALSE, // Normal
  82. GL_TRUE, // Color
  83. GL_FALSE, // Texcoord1
  84. GL_FALSE, // Texcoord2
  85. GL_FALSE, // Cubetexcoord1
  86. GL_FALSE, // Cubetexcoord2
  87. GL_FALSE, // Tangent
  88. GL_FALSE, // Blendweights
  89. GL_FALSE, // Blendindices
  90. GL_FALSE, // Instancematrix1
  91. GL_FALSE, // Instancematrix2
  92. GL_FALSE // Instancematrix3
  93. };
  94. const String VertexBuffer::elementName[] =
  95. {
  96. "Position",
  97. "Normal",
  98. "Color",
  99. "Texcoord1",
  100. "Texcoord2",
  101. "Cubetexcoord1",
  102. "Cubetexcoord2",
  103. "Tangent"
  104. "Blendweights",
  105. "Blendindices",
  106. "Instancematrix1",
  107. "Instancematrix2",
  108. "Instancematrix3"
  109. };
  110. OBJECTTYPESTATIC(VertexBuffer);
  111. VertexBuffer::VertexBuffer(Context* context) :
  112. Object(context),
  113. GPUObject(GetSubsystem<Graphics>()),
  114. discardLockData_(0),
  115. vertexCount_(0),
  116. elementMask_(0),
  117. morphRangeStart_(0),
  118. morphRangeCount_(0),
  119. dynamic_(false),
  120. locked_(false)
  121. {
  122. UpdateOffsets();
  123. }
  124. VertexBuffer::~VertexBuffer()
  125. {
  126. Release();
  127. }
  128. void VertexBuffer::OnDeviceLost()
  129. {
  130. if (object_)
  131. {
  132. void* hwData = Lock(0, vertexCount_, LOCK_READONLY);
  133. if (hwData)
  134. {
  135. saveData_ = new unsigned char[vertexCount_ * vertexSize_];
  136. memcpy(saveData_.Get(), hwData, vertexCount_ * vertexSize_);
  137. }
  138. Unlock();
  139. Release();
  140. }
  141. }
  142. void VertexBuffer::OnDeviceReset()
  143. {
  144. if (!object_)
  145. {
  146. Create();
  147. if (saveData_)
  148. {
  149. SetData(saveData_.Get());
  150. saveData_.Reset();
  151. }
  152. }
  153. }
  154. void VertexBuffer::Release()
  155. {
  156. if (object_)
  157. {
  158. if (!graphics_)
  159. return;
  160. Unlock();
  161. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  162. {
  163. if (graphics_->GetVertexBuffer(i) == this)
  164. graphics_->SetVertexBuffer(0);
  165. }
  166. glDeleteBuffers(1, &object_);
  167. object_ = 0;
  168. }
  169. fallbackData_.Reset();
  170. }
  171. bool VertexBuffer::SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic)
  172. {
  173. dynamic_ = dynamic;
  174. vertexCount_ = vertexCount;
  175. elementMask_ = elementMask;
  176. if (morphRangeStart_ + morphRangeCount_ > vertexCount_)
  177. {
  178. morphRangeStart_ = 0;
  179. morphRangeCount_ = 0;
  180. }
  181. UpdateOffsets();
  182. return Create();
  183. }
  184. bool VertexBuffer::SetData(const void* data)
  185. {
  186. if (!data)
  187. {
  188. LOGERROR("Null pointer for vertex buffer data");
  189. return false;
  190. }
  191. if (locked_)
  192. Unlock();
  193. if (object_)
  194. {
  195. glBindBuffer(GL_ARRAY_BUFFER, object_);
  196. glBufferData(GL_ARRAY_BUFFER, vertexCount_ * vertexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  197. return true;
  198. }
  199. else if (fallbackData_)
  200. {
  201. memcpy(fallbackData_.Get(), data, vertexCount_ * vertexSize_);
  202. return true;
  203. }
  204. return false;
  205. }
  206. bool VertexBuffer::SetDataRange(const void* data, unsigned start, unsigned count)
  207. {
  208. if (!data)
  209. {
  210. LOGERROR("Null pointer for vertex buffer data");
  211. return false;
  212. }
  213. if (start + count > vertexCount_)
  214. {
  215. LOGERROR("Illegal range for setting new index buffer data");
  216. return false;
  217. }
  218. if (locked_)
  219. Unlock();
  220. if (object_)
  221. {
  222. glBindBuffer(GL_ARRAY_BUFFER, object_);
  223. glBufferSubData(GL_ARRAY_BUFFER, start * vertexSize_, count * vertexSize_, data);
  224. return true;
  225. }
  226. else if (fallbackData_)
  227. {
  228. memcpy(fallbackData_.Get() + start * vertexSize_, data, count * vertexSize_);
  229. return true;
  230. }
  231. return false;
  232. }
  233. bool VertexBuffer::SetMorphRange(unsigned start, unsigned count)
  234. {
  235. if (start + count > vertexCount_)
  236. {
  237. LOGERROR("Illegal morph range");
  238. return false;
  239. }
  240. morphRangeStart_ = start;
  241. morphRangeCount_ = count;
  242. return true;
  243. }
  244. void VertexBuffer::SetMorphRangeResetData(const SharedArrayPtr<unsigned char>& data)
  245. {
  246. morphRangeResetData_ = data;
  247. }
  248. void* VertexBuffer::Lock(unsigned start, unsigned count, LockMode mode)
  249. {
  250. if (!object_ && !fallbackData_)
  251. return 0;
  252. if (locked_)
  253. {
  254. LOGERROR("Vertex buffer already locked");
  255. return 0;
  256. }
  257. if (!count || start + count > vertexCount_)
  258. {
  259. LOGERROR("Illegal range for locking vertex buffer");
  260. return 0;
  261. }
  262. void* hwData = 0;
  263. if (object_)
  264. {
  265. GLenum glLockMode = GL_WRITE_ONLY;
  266. if (mode == LOCK_READONLY)
  267. glLockMode = GL_READ_ONLY;
  268. else if (mode == LOCK_NORMAL)
  269. glLockMode = GL_READ_WRITE;
  270. // In discard mode, use a CPU side buffer to avoid stalling
  271. if (mode == LOCK_DISCARD)
  272. {
  273. hwData = discardLockData_ = graphics_->ReserveDiscardLockBuffer(count * vertexSize_);
  274. discardLockStart_ = start;
  275. discardLockCount_ = count;
  276. }
  277. else
  278. {
  279. glBindBuffer(GL_ARRAY_BUFFER, object_);
  280. hwData = glMapBuffer(GL_ARRAY_BUFFER, glLockMode);
  281. if (!hwData)
  282. return 0;
  283. hwData = (unsigned char*)hwData + start * vertexSize_;
  284. }
  285. }
  286. else
  287. hwData = fallbackData_.Get() + start * vertexSize_;
  288. locked_ = true;
  289. return hwData;
  290. }
  291. void VertexBuffer::Unlock()
  292. {
  293. if (locked_)
  294. {
  295. locked_ = false;
  296. if (object_)
  297. {
  298. if (!discardLockData_)
  299. {
  300. glBindBuffer(GL_ARRAY_BUFFER, object_);
  301. glUnmapBuffer(GL_ARRAY_BUFFER);
  302. }
  303. else
  304. {
  305. if (discardLockCount_ < vertexCount_)
  306. SetDataRange(discardLockData_, discardLockStart_, discardLockCount_);
  307. else
  308. SetData(discardLockData_);
  309. graphics_->FreeDiscardLockBuffer(discardLockData_);
  310. discardLockData_ = 0;
  311. }
  312. }
  313. }
  314. }
  315. void* VertexBuffer::LockMorphRange()
  316. {
  317. if (!HasMorphRange())
  318. {
  319. LOGERROR("No vertex morph range defined");
  320. return 0;
  321. }
  322. return Lock(morphRangeStart_, morphRangeCount_, LOCK_DISCARD);
  323. }
  324. void VertexBuffer::ResetMorphRange(void* lockedMorphRange)
  325. {
  326. if (!lockedMorphRange || !morphRangeResetData_)
  327. return;
  328. memcpy(lockedMorphRange, morphRangeResetData_.Get(), morphRangeCount_ * vertexSize_);
  329. }
  330. void VertexBuffer::ClearDataLost()
  331. {
  332. }
  333. void VertexBuffer::UpdateOffsets()
  334. {
  335. unsigned elementOffset = 0;
  336. for (unsigned i = 0; i < MAX_VERTEX_ELEMENTS; ++i)
  337. {
  338. if (elementMask_ & (1 << i))
  339. {
  340. elementOffset_[i] = elementOffset;
  341. elementOffset += elementSize[i];
  342. }
  343. else
  344. elementOffset_[i] = NO_ELEMENT;
  345. }
  346. vertexSize_ = elementOffset;
  347. }
  348. unsigned VertexBuffer::GetVertexSize(unsigned mask)
  349. {
  350. unsigned vertexSize = 0;
  351. for (unsigned i = 0; i < MAX_VERTEX_ELEMENTS; ++i)
  352. {
  353. if (mask & (1 << i))
  354. vertexSize += elementSize[i];
  355. }
  356. return vertexSize;
  357. }
  358. bool VertexBuffer::Create()
  359. {
  360. if (!vertexCount_ || !elementMask_)
  361. {
  362. Release();
  363. return true;
  364. }
  365. if (graphics_)
  366. {
  367. if (!object_)
  368. glGenBuffers(1, &object_);
  369. glBindBuffer(GL_ARRAY_BUFFER, object_);
  370. glBufferData(GL_ARRAY_BUFFER, vertexCount_ * vertexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  371. }
  372. else
  373. fallbackData_ = new unsigned char[vertexCount_ * vertexSize_];
  374. return true;
  375. }