OGLVertexBuffer.cpp 11 KB

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