IndexBuffer.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. // This file contains IndexBuffer code common to all graphics APIs.
  4. #include "../Precompiled.h"
  5. #include "../Graphics/Graphics.h"
  6. #include "../GraphicsAPI/IndexBuffer.h"
  7. #include "../IO/Log.h"
  8. #include "../DebugNew.h"
  9. namespace Urho3D
  10. {
  11. IndexBuffer::IndexBuffer(Context* context, bool forceHeadless) :
  12. Object(context),
  13. GPUObject(forceHeadless ? nullptr : GetSubsystem<Graphics>()),
  14. indexCount_(0),
  15. indexSize_(0),
  16. lockState_(LOCK_NONE),
  17. lockStart_(0),
  18. lockCount_(0),
  19. lockScratchData_(nullptr),
  20. shadowed_(false),
  21. dynamic_(false),
  22. discardLock_(false)
  23. {
  24. // Force shadowing mode if graphics subsystem does not exist
  25. if (!graphics_)
  26. shadowed_ = true;
  27. }
  28. IndexBuffer::~IndexBuffer()
  29. {
  30. Release();
  31. }
  32. void IndexBuffer::SetShadowed(bool enable)
  33. {
  34. // If no graphics subsystem, can not disable shadowing
  35. if (!graphics_)
  36. enable = true;
  37. if (enable != shadowed_)
  38. {
  39. if (enable && indexCount_ && indexSize_)
  40. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  41. else
  42. shadowData_.Reset();
  43. shadowed_ = enable;
  44. }
  45. }
  46. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  47. {
  48. Unlock();
  49. indexCount_ = indexCount;
  50. indexSize_ = (unsigned)(largeIndices ? sizeof(unsigned) : sizeof(unsigned short));
  51. dynamic_ = dynamic;
  52. if (shadowed_ && indexCount_ && indexSize_)
  53. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  54. else
  55. shadowData_.Reset();
  56. return Create();
  57. }
  58. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  59. {
  60. if (!shadowData_)
  61. {
  62. URHO3D_LOGERROR("Used vertex range can only be queried from an index buffer with shadow data");
  63. return false;
  64. }
  65. if (start + count > indexCount_)
  66. {
  67. URHO3D_LOGERROR("Illegal index range for querying used vertices");
  68. return false;
  69. }
  70. minVertex = M_MAX_UNSIGNED;
  71. unsigned maxVertex = 0;
  72. if (indexSize_ == sizeof(unsigned))
  73. {
  74. unsigned* indices = ((unsigned*)shadowData_.Get()) + start;
  75. for (unsigned i = 0; i < count; ++i)
  76. {
  77. if (indices[i] < minVertex)
  78. minVertex = indices[i];
  79. if (indices[i] > maxVertex)
  80. maxVertex = indices[i];
  81. }
  82. }
  83. else
  84. {
  85. unsigned short* indices = ((unsigned short*)shadowData_.Get()) + start;
  86. for (unsigned i = 0; i < count; ++i)
  87. {
  88. if (indices[i] < minVertex)
  89. minVertex = indices[i];
  90. if (indices[i] > maxVertex)
  91. maxVertex = indices[i];
  92. }
  93. }
  94. vertexCount = maxVertex - minVertex + 1;
  95. return true;
  96. }
  97. void IndexBuffer::OnDeviceLost()
  98. {
  99. GAPI gapi = Graphics::GetGAPI();
  100. #ifdef URHO3D_OPENGL
  101. if (gapi == GAPI_OPENGL)
  102. return OnDeviceLost_OGL();
  103. #endif
  104. #ifdef URHO3D_D3D9
  105. if (gapi == GAPI_D3D9)
  106. return OnDeviceLost_D3D9();
  107. #endif
  108. #ifdef URHO3D_D3D11
  109. if (gapi == GAPI_D3D11)
  110. return OnDeviceLost_D3D11();
  111. #endif
  112. }
  113. void IndexBuffer::OnDeviceReset()
  114. {
  115. GAPI gapi = Graphics::GetGAPI();
  116. #ifdef URHO3D_OPENGL
  117. if (gapi == GAPI_OPENGL)
  118. return OnDeviceReset_OGL();
  119. #endif
  120. #ifdef URHO3D_D3D9
  121. if (gapi == GAPI_D3D9)
  122. return OnDeviceReset_D3D9();
  123. #endif
  124. #ifdef URHO3D_D3D11
  125. if (gapi == GAPI_D3D11)
  126. return OnDeviceReset_D3D11();
  127. #endif
  128. }
  129. void IndexBuffer::Release()
  130. {
  131. GAPI gapi = Graphics::GetGAPI();
  132. #ifdef URHO3D_OPENGL
  133. if (gapi == GAPI_OPENGL)
  134. return Release_OGL();
  135. #endif
  136. #ifdef URHO3D_D3D9
  137. if (gapi == GAPI_D3D9)
  138. return Release_D3D9();
  139. #endif
  140. #ifdef URHO3D_D3D11
  141. if (gapi == GAPI_D3D11)
  142. return Release_D3D11();
  143. #endif
  144. }
  145. bool IndexBuffer::SetData(const void* data)
  146. {
  147. GAPI gapi = Graphics::GetGAPI();
  148. #ifdef URHO3D_OPENGL
  149. if (gapi == GAPI_OPENGL)
  150. return SetData_OGL(data);
  151. #endif
  152. #ifdef URHO3D_D3D9
  153. if (gapi == GAPI_D3D9)
  154. return SetData_D3D9(data);;
  155. #endif
  156. #ifdef URHO3D_D3D11
  157. if (gapi == GAPI_D3D11)
  158. return SetData_D3D11(data);;
  159. #endif
  160. return {}; // Prevent warning
  161. }
  162. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  163. {
  164. GAPI gapi = Graphics::GetGAPI();
  165. #ifdef URHO3D_OPENGL
  166. if (gapi == GAPI_OPENGL)
  167. return SetDataRange_OGL(data, start, count, discard);
  168. #endif
  169. #ifdef URHO3D_D3D9
  170. if (gapi == GAPI_D3D9)
  171. return SetDataRange_D3D9(data, start, count, discard);
  172. #endif
  173. #ifdef URHO3D_D3D11
  174. if (gapi == GAPI_D3D11)
  175. return SetDataRange_D3D11(data, start, count, discard);
  176. #endif
  177. return {}; // Prevent warning
  178. }
  179. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  180. {
  181. GAPI gapi = Graphics::GetGAPI();
  182. #ifdef URHO3D_OPENGL
  183. if (gapi == GAPI_OPENGL)
  184. return Lock_OGL(start, count, discard);
  185. #endif
  186. #ifdef URHO3D_D3D9
  187. if (gapi == GAPI_D3D9)
  188. return Lock_D3D9(start, count, discard);
  189. #endif
  190. #ifdef URHO3D_D3D11
  191. if (gapi == GAPI_D3D11)
  192. return Lock_D3D11(start, count, discard);
  193. #endif
  194. return {}; // Prevent warning
  195. }
  196. void IndexBuffer::Unlock()
  197. {
  198. GAPI gapi = Graphics::GetGAPI();
  199. #ifdef URHO3D_OPENGL
  200. if (gapi == GAPI_OPENGL)
  201. return Unlock_OGL();
  202. #endif
  203. #ifdef URHO3D_D3D9
  204. if (gapi == GAPI_D3D9)
  205. return Unlock_D3D9();
  206. #endif
  207. #ifdef URHO3D_D3D11
  208. if (gapi == GAPI_D3D11)
  209. return Unlock_D3D11();
  210. #endif
  211. }
  212. bool IndexBuffer::Create()
  213. {
  214. GAPI gapi = Graphics::GetGAPI();
  215. #ifdef URHO3D_OPENGL
  216. if (gapi == GAPI_OPENGL)
  217. return Create_OGL();
  218. #endif
  219. #ifdef URHO3D_D3D9
  220. if (gapi == GAPI_D3D9)
  221. return Create_D3D9();
  222. #endif
  223. #ifdef URHO3D_D3D11
  224. if (gapi == GAPI_D3D11)
  225. return Create_D3D11();
  226. #endif
  227. return {}; // Prevent warning
  228. }
  229. bool IndexBuffer::UpdateToGPU()
  230. {
  231. GAPI gapi = Graphics::GetGAPI();
  232. #ifdef URHO3D_OPENGL
  233. if (gapi == GAPI_OPENGL)
  234. return UpdateToGPU_OGL();
  235. #endif
  236. #ifdef URHO3D_D3D9
  237. if (gapi == GAPI_D3D9)
  238. return UpdateToGPU_D3D9();
  239. #endif
  240. #ifdef URHO3D_D3D11
  241. if (gapi == GAPI_D3D11)
  242. return UpdateToGPU_D3D11();
  243. #endif
  244. return {}; // Prevent warning
  245. }
  246. void* IndexBuffer::MapBuffer(unsigned start, unsigned count, bool discard)
  247. {
  248. GAPI gapi = Graphics::GetGAPI();
  249. #ifdef URHO3D_OPENGL
  250. if (gapi == GAPI_OPENGL)
  251. return MapBuffer_OGL(start, count, discard);
  252. #endif
  253. #ifdef URHO3D_D3D9
  254. if (gapi == GAPI_D3D9)
  255. return MapBuffer_D3D9(start, count, discard);
  256. #endif
  257. #ifdef URHO3D_D3D11
  258. if (gapi == GAPI_D3D11)
  259. return MapBuffer_D3D11(start, count, discard);
  260. #endif
  261. return {}; // Prevent warning
  262. }
  263. void IndexBuffer::UnmapBuffer()
  264. {
  265. GAPI gapi = Graphics::GetGAPI();
  266. #ifdef URHO3D_OPENGL
  267. if (gapi == GAPI_OPENGL)
  268. return UnmapBuffer_OGL();
  269. #endif
  270. #ifdef URHO3D_D3D9
  271. if (gapi == GAPI_D3D9)
  272. return UnmapBuffer_D3D9();
  273. #endif
  274. #ifdef URHO3D_D3D11
  275. if (gapi == GAPI_D3D11)
  276. return UnmapBuffer_D3D11();
  277. #endif
  278. }
  279. }