OGLVertexBuffer.cpp 11 KB

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