OGLVertexBuffer.cpp 11 KB

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