2
0

IndexBuffer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 u8[(size_t)indexCount_ * indexSize_];
  41. else
  42. shadowData_.Reset();
  43. shadowed_ = enable;
  44. }
  45. }
  46. bool IndexBuffer::SetSize(i32 indexCount, bool largeIndices, bool dynamic)
  47. {
  48. assert(indexCount >= 0);
  49. Unlock();
  50. indexCount_ = indexCount;
  51. indexSize_ = (i32)(largeIndices ? sizeof(u32) : sizeof(u16));
  52. dynamic_ = dynamic;
  53. if (shadowed_ && indexCount_ && indexSize_)
  54. shadowData_ = new u8[(size_t)indexCount_ * indexSize_];
  55. else
  56. shadowData_.Reset();
  57. return Create();
  58. }
  59. bool IndexBuffer::GetUsedVertexRange(i32 start, i32 count, i32& minVertex, i32& vertexCount)
  60. {
  61. assert(start >= 0 && count >= 0);
  62. if (!shadowData_)
  63. {
  64. URHO3D_LOGERROR("Used vertex range can only be queried from an index buffer with shadow data");
  65. return false;
  66. }
  67. if (start + count > indexCount_)
  68. {
  69. URHO3D_LOGERROR("Illegal index range for querying used vertices");
  70. return false;
  71. }
  72. minVertex = M_MAX_INT;
  73. i32 maxVertex = 0;
  74. if (indexSize_ == sizeof(u32))
  75. {
  76. u32* indices = (u32*)shadowData_.Get() + start;
  77. for (i32 i = 0; i < count; ++i)
  78. {
  79. i32 index = (i32)indices[i];
  80. if (index < minVertex)
  81. minVertex = index;
  82. if (index > maxVertex)
  83. maxVertex = index;
  84. }
  85. }
  86. else
  87. {
  88. u16* indices = (u16*)shadowData_.Get() + start;
  89. for (i32 i = 0; i < count; ++i)
  90. {
  91. i32 index = (i32)indices[i];
  92. if (index < minVertex)
  93. minVertex = index;
  94. if (index > maxVertex)
  95. maxVertex = index;
  96. }
  97. }
  98. vertexCount = maxVertex - minVertex + 1;
  99. return true;
  100. }
  101. void IndexBuffer::OnDeviceLost()
  102. {
  103. GAPI gapi = Graphics::GetGAPI();
  104. #ifdef URHO3D_OPENGL
  105. if (gapi == GAPI_OPENGL)
  106. return OnDeviceLost_OGL();
  107. #endif
  108. #ifdef URHO3D_D3D9
  109. if (gapi == GAPI_D3D9)
  110. return OnDeviceLost_D3D9();
  111. #endif
  112. #ifdef URHO3D_D3D11
  113. if (gapi == GAPI_D3D11)
  114. return OnDeviceLost_D3D11();
  115. #endif
  116. }
  117. void IndexBuffer::OnDeviceReset()
  118. {
  119. GAPI gapi = Graphics::GetGAPI();
  120. #ifdef URHO3D_OPENGL
  121. if (gapi == GAPI_OPENGL)
  122. return OnDeviceReset_OGL();
  123. #endif
  124. #ifdef URHO3D_D3D9
  125. if (gapi == GAPI_D3D9)
  126. return OnDeviceReset_D3D9();
  127. #endif
  128. #ifdef URHO3D_D3D11
  129. if (gapi == GAPI_D3D11)
  130. return OnDeviceReset_D3D11();
  131. #endif
  132. }
  133. void IndexBuffer::Release()
  134. {
  135. GAPI gapi = Graphics::GetGAPI();
  136. #ifdef URHO3D_OPENGL
  137. if (gapi == GAPI_OPENGL)
  138. return Release_OGL();
  139. #endif
  140. #ifdef URHO3D_D3D9
  141. if (gapi == GAPI_D3D9)
  142. return Release_D3D9();
  143. #endif
  144. #ifdef URHO3D_D3D11
  145. if (gapi == GAPI_D3D11)
  146. return Release_D3D11();
  147. #endif
  148. }
  149. bool IndexBuffer::SetData(const void* data)
  150. {
  151. GAPI gapi = Graphics::GetGAPI();
  152. #ifdef URHO3D_OPENGL
  153. if (gapi == GAPI_OPENGL)
  154. return SetData_OGL(data);
  155. #endif
  156. #ifdef URHO3D_D3D9
  157. if (gapi == GAPI_D3D9)
  158. return SetData_D3D9(data);;
  159. #endif
  160. #ifdef URHO3D_D3D11
  161. if (gapi == GAPI_D3D11)
  162. return SetData_D3D11(data);;
  163. #endif
  164. return {}; // Prevent warning
  165. }
  166. bool IndexBuffer::SetDataRange(const void* data, i32 start, i32 count, bool discard)
  167. {
  168. assert(start >= 0 && count >= 0);
  169. GAPI gapi = Graphics::GetGAPI();
  170. #ifdef URHO3D_OPENGL
  171. if (gapi == GAPI_OPENGL)
  172. return SetDataRange_OGL(data, start, count, discard);
  173. #endif
  174. #ifdef URHO3D_D3D9
  175. if (gapi == GAPI_D3D9)
  176. return SetDataRange_D3D9(data, start, count, discard);
  177. #endif
  178. #ifdef URHO3D_D3D11
  179. if (gapi == GAPI_D3D11)
  180. return SetDataRange_D3D11(data, start, count, discard);
  181. #endif
  182. return {}; // Prevent warning
  183. }
  184. void* IndexBuffer::Lock(i32 start, i32 count, bool discard)
  185. {
  186. assert(start >= 0 && count >= 0);
  187. GAPI gapi = Graphics::GetGAPI();
  188. #ifdef URHO3D_OPENGL
  189. if (gapi == GAPI_OPENGL)
  190. return Lock_OGL(start, count, discard);
  191. #endif
  192. #ifdef URHO3D_D3D9
  193. if (gapi == GAPI_D3D9)
  194. return Lock_D3D9(start, count, discard);
  195. #endif
  196. #ifdef URHO3D_D3D11
  197. if (gapi == GAPI_D3D11)
  198. return Lock_D3D11(start, count, discard);
  199. #endif
  200. return {}; // Prevent warning
  201. }
  202. void IndexBuffer::Unlock()
  203. {
  204. GAPI gapi = Graphics::GetGAPI();
  205. #ifdef URHO3D_OPENGL
  206. if (gapi == GAPI_OPENGL)
  207. return Unlock_OGL();
  208. #endif
  209. #ifdef URHO3D_D3D9
  210. if (gapi == GAPI_D3D9)
  211. return Unlock_D3D9();
  212. #endif
  213. #ifdef URHO3D_D3D11
  214. if (gapi == GAPI_D3D11)
  215. return Unlock_D3D11();
  216. #endif
  217. }
  218. bool IndexBuffer::Create()
  219. {
  220. GAPI gapi = Graphics::GetGAPI();
  221. #ifdef URHO3D_OPENGL
  222. if (gapi == GAPI_OPENGL)
  223. return Create_OGL();
  224. #endif
  225. #ifdef URHO3D_D3D9
  226. if (gapi == GAPI_D3D9)
  227. return Create_D3D9();
  228. #endif
  229. #ifdef URHO3D_D3D11
  230. if (gapi == GAPI_D3D11)
  231. return Create_D3D11();
  232. #endif
  233. return {}; // Prevent warning
  234. }
  235. bool IndexBuffer::UpdateToGPU()
  236. {
  237. GAPI gapi = Graphics::GetGAPI();
  238. #ifdef URHO3D_OPENGL
  239. if (gapi == GAPI_OPENGL)
  240. return UpdateToGPU_OGL();
  241. #endif
  242. #ifdef URHO3D_D3D9
  243. if (gapi == GAPI_D3D9)
  244. return UpdateToGPU_D3D9();
  245. #endif
  246. #ifdef URHO3D_D3D11
  247. if (gapi == GAPI_D3D11)
  248. return UpdateToGPU_D3D11();
  249. #endif
  250. return {}; // Prevent warning
  251. }
  252. void* IndexBuffer::MapBuffer(i32 start, i32 count, bool discard)
  253. {
  254. assert(start >= 0 && count >= 0);
  255. GAPI gapi = Graphics::GetGAPI();
  256. #ifdef URHO3D_OPENGL
  257. if (gapi == GAPI_OPENGL)
  258. return MapBuffer_OGL(start, count, discard);
  259. #endif
  260. #ifdef URHO3D_D3D9
  261. if (gapi == GAPI_D3D9)
  262. return MapBuffer_D3D9(start, count, discard);
  263. #endif
  264. #ifdef URHO3D_D3D11
  265. if (gapi == GAPI_D3D11)
  266. return MapBuffer_D3D11(start, count, discard);
  267. #endif
  268. return {}; // Prevent warning
  269. }
  270. void IndexBuffer::UnmapBuffer()
  271. {
  272. GAPI gapi = Graphics::GetGAPI();
  273. #ifdef URHO3D_OPENGL
  274. if (gapi == GAPI_OPENGL)
  275. return UnmapBuffer_OGL();
  276. #endif
  277. #ifdef URHO3D_D3D9
  278. if (gapi == GAPI_D3D9)
  279. return UnmapBuffer_D3D9();
  280. #endif
  281. #ifdef URHO3D_D3D11
  282. if (gapi == GAPI_D3D11)
  283. return UnmapBuffer_D3D11();
  284. #endif
  285. }
  286. }