VertexBuffer.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. // This file contains VertexBuffer code common to all graphics APIs.
  4. #include "../Precompiled.h"
  5. #include "../Graphics/Graphics.h"
  6. #include "../Math/MathDefs.h"
  7. #include "VertexBuffer.h"
  8. #include "../DebugNew.h"
  9. namespace Urho3D
  10. {
  11. VertexBuffer::VertexBuffer(Context* context, bool forceHeadless) :
  12. Object(context),
  13. GPUObject(forceHeadless ? nullptr : GetSubsystem<Graphics>())
  14. {
  15. UpdateOffsets();
  16. // Force shadowing mode if graphics subsystem does not exist
  17. if (!graphics_)
  18. shadowed_ = true;
  19. }
  20. VertexBuffer::~VertexBuffer()
  21. {
  22. Release();
  23. }
  24. void VertexBuffer::SetShadowed(bool enable)
  25. {
  26. // If no graphics subsystem, can not disable shadowing
  27. if (!graphics_)
  28. enable = true;
  29. if (enable != shadowed_)
  30. {
  31. if (enable && vertexSize_ && vertexCount_)
  32. shadowData_ = new u8[vertexCount_ * vertexSize_];
  33. else
  34. shadowData_.Reset();
  35. shadowed_ = enable;
  36. }
  37. }
  38. bool VertexBuffer::SetSize(unsigned vertexCount, unsigned elementMask, bool dynamic)
  39. {
  40. return SetSize(vertexCount, GetElements(elementMask), dynamic);
  41. }
  42. bool VertexBuffer::SetSize(unsigned vertexCount, const Vector<VertexElement>& elements, bool dynamic)
  43. {
  44. Unlock();
  45. vertexCount_ = vertexCount;
  46. elements_ = elements;
  47. dynamic_ = dynamic;
  48. UpdateOffsets();
  49. if (shadowed_ && vertexCount_ && vertexSize_)
  50. shadowData_ = new u8[vertexCount_ * vertexSize_];
  51. else
  52. shadowData_.Reset();
  53. return Create();
  54. }
  55. void VertexBuffer::UpdateOffsets()
  56. {
  57. unsigned elementOffset = 0;
  58. elementHash_ = 0;
  59. elementMask_ = MASK_NONE;
  60. for (Vector<VertexElement>::Iterator i = elements_.Begin(); i != elements_.End(); ++i)
  61. {
  62. i->offset_ = elementOffset;
  63. elementOffset += ELEMENT_TYPESIZES[i->type_];
  64. elementHash_ <<= 6;
  65. elementHash_ += (((int)i->type_ + 1) * ((int)i->semantic_ + 1) + i->index_);
  66. for (unsigned j = 0; j < MAX_LEGACY_VERTEX_ELEMENTS; ++j)
  67. {
  68. const VertexElement& legacy = LEGACY_VERTEXELEMENTS[j];
  69. if (i->type_ == legacy.type_ && i->semantic_ == legacy.semantic_ && i->index_ == legacy.index_)
  70. elementMask_ |= VertexMaskFlags(1u << j);
  71. }
  72. }
  73. vertexSize_ = elementOffset;
  74. }
  75. const VertexElement* VertexBuffer::GetElement(VertexElementSemantic semantic, i8 index/* = 0*/) const
  76. {
  77. assert(index >= 0);
  78. for (const VertexElement& element : elements_)
  79. {
  80. if (element.semantic_ == semantic && element.index_ == index)
  81. return &element;
  82. }
  83. return nullptr;
  84. }
  85. const VertexElement* VertexBuffer::GetElement(VertexElementType type, VertexElementSemantic semantic, i8 index/* = 0*/) const
  86. {
  87. assert(index >= 0);
  88. for (const VertexElement& element : elements_)
  89. {
  90. if (element.type_ == type && element.semantic_ == semantic && element.index_ == index)
  91. return &element;
  92. }
  93. return nullptr;
  94. }
  95. const VertexElement* VertexBuffer::GetElement(const Vector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, i8 index/* = 0*/)
  96. {
  97. assert(index >= 0);
  98. for (const VertexElement& element : elements)
  99. {
  100. if (element.type_ == type && element.semantic_ == semantic && element.index_ == index)
  101. return &element;
  102. }
  103. return nullptr;
  104. }
  105. bool VertexBuffer::HasElement(const Vector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, i8 index/* = 0*/)
  106. {
  107. assert(index >= 0);
  108. return GetElement(elements, type, semantic, index) != nullptr;
  109. }
  110. i32 VertexBuffer::GetElementOffset(const Vector<VertexElement>& elements, VertexElementType type, VertexElementSemantic semantic, i8 index/* = 0*/)
  111. {
  112. assert(index >= 0);
  113. const VertexElement* element = GetElement(elements, type, semantic, index);
  114. return element ? element->offset_ : NINDEX;
  115. }
  116. Vector<VertexElement> VertexBuffer::GetElements(unsigned elementMask)
  117. {
  118. Vector<VertexElement> ret;
  119. for (unsigned i = 0; i < MAX_LEGACY_VERTEX_ELEMENTS; ++i)
  120. {
  121. if (elementMask & (1u << i))
  122. ret.Push(LEGACY_VERTEXELEMENTS[i]);
  123. }
  124. return ret;
  125. }
  126. unsigned VertexBuffer::GetVertexSize(const Vector<VertexElement>& elements)
  127. {
  128. unsigned size = 0;
  129. for (i32 i = 0; i < elements.Size(); ++i)
  130. size += ELEMENT_TYPESIZES[elements[i].type_];
  131. return size;
  132. }
  133. unsigned VertexBuffer::GetVertexSize(unsigned elementMask)
  134. {
  135. unsigned size = 0;
  136. for (unsigned i = 0; i < MAX_LEGACY_VERTEX_ELEMENTS; ++i)
  137. {
  138. if (elementMask & (1u << i))
  139. size += ELEMENT_TYPESIZES[LEGACY_VERTEXELEMENTS[i].type_];
  140. }
  141. return size;
  142. }
  143. void VertexBuffer::UpdateOffsets(Vector<VertexElement>& elements)
  144. {
  145. unsigned elementOffset = 0;
  146. for (Vector<VertexElement>::Iterator i = elements.Begin(); i != elements.End(); ++i)
  147. {
  148. i->offset_ = elementOffset;
  149. elementOffset += ELEMENT_TYPESIZES[i->type_];
  150. }
  151. }
  152. void VertexBuffer::OnDeviceLost()
  153. {
  154. GAPI gapi = Graphics::GetGAPI();
  155. #ifdef URHO3D_OPENGL
  156. if (gapi == GAPI_OPENGL)
  157. return OnDeviceLost_OGL();
  158. #endif
  159. #ifdef URHO3D_D3D9
  160. if (gapi == GAPI_D3D9)
  161. return OnDeviceLost_D3D9();
  162. #endif
  163. #ifdef URHO3D_D3D11
  164. if (gapi == GAPI_D3D11)
  165. return OnDeviceLost_D3D11();
  166. #endif
  167. }
  168. void VertexBuffer::OnDeviceReset()
  169. {
  170. GAPI gapi = Graphics::GetGAPI();
  171. #ifdef URHO3D_OPENGL
  172. if (gapi == GAPI_OPENGL)
  173. return OnDeviceReset_OGL();
  174. #endif
  175. #ifdef URHO3D_D3D9
  176. if (gapi == GAPI_D3D9)
  177. return OnDeviceReset_D3D9();
  178. #endif
  179. #ifdef URHO3D_D3D11
  180. if (gapi == GAPI_D3D11)
  181. return OnDeviceReset_D3D11();
  182. #endif
  183. }
  184. void VertexBuffer::Release()
  185. {
  186. GAPI gapi = Graphics::GetGAPI();
  187. #ifdef URHO3D_OPENGL
  188. if (gapi == GAPI_OPENGL)
  189. return Release_OGL();
  190. #endif
  191. #ifdef URHO3D_D3D9
  192. if (gapi == GAPI_D3D9)
  193. return Release_D3D9();
  194. #endif
  195. #ifdef URHO3D_D3D11
  196. if (gapi == GAPI_D3D11)
  197. return Release_D3D11();
  198. #endif
  199. }
  200. bool VertexBuffer::SetData(const void* data)
  201. {
  202. GAPI gapi = Graphics::GetGAPI();
  203. #ifdef URHO3D_OPENGL
  204. if (gapi == GAPI_OPENGL)
  205. return SetData_OGL(data);
  206. #endif
  207. #ifdef URHO3D_D3D9
  208. if (gapi == GAPI_D3D9)
  209. return SetData_D3D9(data);
  210. #endif
  211. #ifdef URHO3D_D3D11
  212. if (gapi == GAPI_D3D11)
  213. return SetData_D3D11(data);
  214. #endif
  215. return {}; // Prevent warning
  216. }
  217. bool VertexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  218. {
  219. GAPI gapi = Graphics::GetGAPI();
  220. #ifdef URHO3D_OPENGL
  221. if (gapi == GAPI_OPENGL)
  222. return SetDataRange_OGL(data, start, count, discard);
  223. #endif
  224. #ifdef URHO3D_D3D9
  225. if (gapi == GAPI_D3D9)
  226. return SetDataRange_D3D9(data, start, count, discard);
  227. #endif
  228. #ifdef URHO3D_D3D11
  229. if (gapi == GAPI_D3D11)
  230. return SetDataRange_D3D11(data, start, count, discard);
  231. #endif
  232. return {}; // Prevent warning
  233. }
  234. void* VertexBuffer::Lock(unsigned start, unsigned count, bool discard)
  235. {
  236. GAPI gapi = Graphics::GetGAPI();
  237. #ifdef URHO3D_OPENGL
  238. if (gapi == GAPI_OPENGL)
  239. return Lock_OGL(start, count, discard);
  240. #endif
  241. #ifdef URHO3D_D3D9
  242. if (gapi == GAPI_D3D9)
  243. return Lock_D3D9(start, count, discard);
  244. #endif
  245. #ifdef URHO3D_D3D11
  246. if (gapi == GAPI_D3D11)
  247. return Lock_D3D11(start, count, discard);
  248. #endif
  249. return {}; // Prevent warning
  250. }
  251. void VertexBuffer::Unlock()
  252. {
  253. GAPI gapi = Graphics::GetGAPI();
  254. #ifdef URHO3D_OPENGL
  255. if (gapi == GAPI_OPENGL)
  256. return Unlock_OGL();
  257. #endif
  258. #ifdef URHO3D_D3D9
  259. if (gapi == GAPI_D3D9)
  260. return Unlock_D3D9();
  261. #endif
  262. #ifdef URHO3D_D3D11
  263. if (gapi == GAPI_D3D11)
  264. return Unlock_D3D11();
  265. #endif
  266. }
  267. bool VertexBuffer::Create()
  268. {
  269. GAPI gapi = Graphics::GetGAPI();
  270. #ifdef URHO3D_OPENGL
  271. if (gapi == GAPI_OPENGL)
  272. return Create_OGL();
  273. #endif
  274. #ifdef URHO3D_D3D9
  275. if (gapi == GAPI_D3D9)
  276. return Create_D3D9();
  277. #endif
  278. #ifdef URHO3D_D3D11
  279. if (gapi == GAPI_D3D11)
  280. return Create_D3D11();
  281. #endif
  282. return {}; // Prevent warning
  283. }
  284. bool VertexBuffer::UpdateToGPU()
  285. {
  286. GAPI gapi = Graphics::GetGAPI();
  287. #ifdef URHO3D_OPENGL
  288. if (gapi == GAPI_OPENGL)
  289. return UpdateToGPU_OGL();
  290. #endif
  291. #ifdef URHO3D_D3D9
  292. if (gapi == GAPI_D3D9)
  293. return UpdateToGPU_D3D9();
  294. #endif
  295. #ifdef URHO3D_D3D11
  296. if (gapi == GAPI_D3D11)
  297. return UpdateToGPU_D3D11();
  298. #endif
  299. return {}; // Prevent warning
  300. }
  301. void* VertexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
  302. {
  303. GAPI gapi = Graphics::GetGAPI();
  304. #ifdef URHO3D_OPENGL
  305. if (gapi == GAPI_OPENGL)
  306. return MapBuffer_OGL(start, count, discard);
  307. #endif
  308. #ifdef URHO3D_D3D9
  309. if (gapi == GAPI_D3D9)
  310. return MapBuffer_D3D9(start, count, discard);
  311. #endif
  312. #ifdef URHO3D_D3D11
  313. if (gapi == GAPI_D3D11)
  314. return MapBuffer_D3D11(start, count, discard);
  315. #endif
  316. return {}; // Prevent warning
  317. }
  318. void VertexBuffer::UnmapBuffer()
  319. {
  320. GAPI gapi = Graphics::GetGAPI();
  321. #ifdef URHO3D_OPENGL
  322. if (gapi == GAPI_OPENGL)
  323. return UnmapBuffer_OGL();
  324. #endif
  325. #ifdef URHO3D_D3D9
  326. if (gapi == GAPI_D3D9)
  327. return UnmapBuffer_D3D9();
  328. #endif
  329. #ifdef URHO3D_D3D11
  330. if (gapi == GAPI_D3D11)
  331. return UnmapBuffer_D3D11();
  332. #endif
  333. }
  334. }