OGLVertexBuffer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 <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. vertexCount_(0),
  115. elementMask_(0),
  116. lockState_(LOCK_NONE),
  117. lockStart_(0),
  118. lockCount_(0),
  119. lockScratchData_(0),
  120. shadowed_(false),
  121. dynamic_(false)
  122. {
  123. UpdateOffsets();
  124. // Force shadowing mode if graphics subsystem does not exist
  125. if (!graphics_)
  126. shadowed_ = true;
  127. }
  128. VertexBuffer::~VertexBuffer()
  129. {
  130. Release();
  131. }
  132. void VertexBuffer::OnDeviceLost()
  133. {
  134. GPUObject::OnDeviceLost();
  135. }
  136. void VertexBuffer::OnDeviceReset()
  137. {
  138. if (!object_)
  139. {
  140. Create();
  141. dataLost_ = !UpdateToGPU();
  142. }
  143. else if (dataPending_)
  144. dataLost_ = !UpdateToGPU();
  145. dataPending_ = false;
  146. }
  147. void VertexBuffer::Release()
  148. {
  149. Unlock();
  150. if (object_)
  151. {
  152. if (!graphics_ || graphics_->IsDeviceLost())
  153. return;
  154. for (unsigned i = 0; i < MAX_VERTEX_STREAMS; ++i)
  155. {
  156. if (graphics_->GetVertexBuffer(i) == this)
  157. graphics_->SetVertexBuffer(0);
  158. }
  159. glDeleteBuffers(1, &object_);
  160. object_ = 0;
  161. }
  162. }
  163. void VertexBuffer::SetShadowed(bool enable)
  164. {
  165. // If no graphics subsystem, can not disable shadowing
  166. if (!graphics_)
  167. enable = true;
  168. if (enable != shadowed_)
  169. {
  170. if (enable && vertexSize_ && vertexCount_)
  171. shadowData_ = new unsigned char[vertexCount_ * vertexSize_];
  172. else
  173. shadowData_.Reset();
  174. shadowed_ = enable;
  175. }
  176. }
  177. bool VertexBuffer::SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic)
  178. {
  179. Unlock();
  180. dynamic_ = dynamic;
  181. vertexCount_ = vertexCount;
  182. elementMask_ = elementMask;
  183. UpdateOffsets();
  184. if (shadowed_ && vertexCount_ && vertexSize_)
  185. shadowData_ = new unsigned char[vertexCount_ * vertexSize_];
  186. else
  187. shadowData_.Reset();
  188. return Create();
  189. }
  190. bool VertexBuffer::SetData(const void* data)
  191. {
  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 (shadowData_ && data != shadowData_.Get())
  203. memcpy(shadowData_.Get(), data, vertexCount_ * vertexSize_);
  204. if (object_)
  205. {
  206. if (!graphics_->IsDeviceLost())
  207. {
  208. glBindBuffer(GL_ARRAY_BUFFER, object_);
  209. glBufferData(GL_ARRAY_BUFFER, vertexCount_ * vertexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  210. }
  211. else
  212. {
  213. LOGWARNING("Vertex buffer data assignment while device is lost");
  214. dataPending_ = true;
  215. }
  216. }
  217. dataLost_ = false;
  218. return true;
  219. }
  220. bool VertexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  221. {
  222. if (start == 0 && count == vertexCount_)
  223. return SetData(data);
  224. if (!data)
  225. {
  226. LOGERROR("Null pointer for vertex buffer data");
  227. return false;
  228. }
  229. if (!vertexSize_)
  230. {
  231. LOGERROR("Vertex elements not defined, can not set vertex buffer data");
  232. return false;
  233. }
  234. if (start + count > vertexCount_)
  235. {
  236. LOGERROR("Illegal range for setting new vertex buffer data");
  237. return false;
  238. }
  239. if (!count)
  240. return true;
  241. if (shadowData_ && shadowData_.Get() + start * vertexSize_ != data)
  242. memcpy(shadowData_.Get() + start * vertexSize_, data, count * vertexSize_);
  243. if (object_)
  244. {
  245. if (!graphics_->IsDeviceLost())
  246. {
  247. glBindBuffer(GL_ARRAY_BUFFER, object_);
  248. if (!discard || start != 0)
  249. glBufferSubData(GL_ARRAY_BUFFER, start * vertexSize_, count * vertexSize_, data);
  250. else
  251. glBufferData(GL_ARRAY_BUFFER, count * vertexSize_, data, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  252. }
  253. else
  254. {
  255. LOGWARNING("Vertex buffer data assignment while device is lost");
  256. dataPending_ = true;
  257. }
  258. }
  259. return true;
  260. }
  261. void* VertexBuffer::Lock(unsigned start, unsigned count, bool discard)
  262. {
  263. if (lockState_ != LOCK_NONE)
  264. {
  265. LOGERROR("Vertex buffer already locked");
  266. return 0;
  267. }
  268. if (!vertexSize_)
  269. {
  270. LOGERROR("Vertex elements not defined, can not lock vertex buffer");
  271. return 0;
  272. }
  273. if (start + count > vertexCount_)
  274. {
  275. LOGERROR("Illegal range for locking vertex buffer");
  276. return 0;
  277. }
  278. if (!count)
  279. return 0;
  280. lockStart_ = start;
  281. lockCount_ = count;
  282. if (shadowData_)
  283. {
  284. lockState_ = LOCK_SHADOW;
  285. return shadowData_.Get() + start * vertexSize_;
  286. }
  287. else if (graphics_)
  288. {
  289. lockState_ = LOCK_SCRATCH;
  290. lockScratchData_ = graphics_->ReserveScratchBuffer(count * vertexSize_);
  291. return lockScratchData_;
  292. }
  293. else
  294. return 0;
  295. }
  296. void VertexBuffer::Unlock()
  297. {
  298. switch (lockState_)
  299. {
  300. case LOCK_SHADOW:
  301. SetDataRange(shadowData_.Get() + lockStart_ * vertexSize_, lockStart_, lockCount_);
  302. lockState_ = LOCK_NONE;
  303. break;
  304. case LOCK_SCRATCH:
  305. SetDataRange(lockScratchData_, lockStart_, lockCount_);
  306. if (graphics_)
  307. graphics_->FreeScratchBuffer(lockScratchData_);
  308. lockScratchData_ = 0;
  309. lockState_ = LOCK_NONE;
  310. break;
  311. }
  312. }
  313. void VertexBuffer::UpdateOffsets()
  314. {
  315. unsigned elementOffset = 0;
  316. for (unsigned i = 0; i < MAX_VERTEX_ELEMENTS; ++i)
  317. {
  318. if (elementMask_ & (1 << i))
  319. {
  320. elementOffset_[i] = elementOffset;
  321. elementOffset += elementSize[i];
  322. }
  323. else
  324. elementOffset_[i] = NO_ELEMENT;
  325. }
  326. vertexSize_ = elementOffset;
  327. }
  328. unsigned VertexBuffer::GetVertexSize(unsigned mask)
  329. {
  330. unsigned vertexSize = 0;
  331. for (unsigned i = 0; i < MAX_VERTEX_ELEMENTS; ++i)
  332. {
  333. if (mask & (1 << i))
  334. vertexSize += elementSize[i];
  335. }
  336. return vertexSize;
  337. }
  338. bool VertexBuffer::Create()
  339. {
  340. if (!vertexCount_ || !elementMask_)
  341. {
  342. Release();
  343. return true;
  344. }
  345. if (graphics_)
  346. {
  347. if (graphics_->IsDeviceLost())
  348. {
  349. LOGWARNING("Vertex buffer creation while device is lost");
  350. return false;
  351. }
  352. if (!object_)
  353. glGenBuffers(1, &object_);
  354. if (!object_)
  355. {
  356. LOGERROR("Failed to create vertex buffer");
  357. return false;
  358. }
  359. glBindBuffer(GL_ARRAY_BUFFER, object_);
  360. glBufferData(GL_ARRAY_BUFFER, vertexCount_ * vertexSize_, 0, dynamic_ ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  361. }
  362. return true;
  363. }
  364. bool VertexBuffer::UpdateToGPU()
  365. {
  366. if (object_ && shadowData_)
  367. return SetData(shadowData_.Get());
  368. else
  369. return false;
  370. }