IndexBuffer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // Copyright (c) 2008-2023 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 byte[(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 byte[(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_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_D3D11
  121. if (gapi == GAPI_D3D11)
  122. return OnDeviceReset_D3D11();
  123. #endif
  124. }
  125. void IndexBuffer::Release()
  126. {
  127. GAPI gapi = Graphics::GetGAPI();
  128. #ifdef URHO3D_OPENGL
  129. if (gapi == GAPI_OPENGL)
  130. return Release_OGL();
  131. #endif
  132. #ifdef URHO3D_D3D11
  133. if (gapi == GAPI_D3D11)
  134. return Release_D3D11();
  135. #endif
  136. }
  137. bool IndexBuffer::SetData(const void* data)
  138. {
  139. GAPI gapi = Graphics::GetGAPI();
  140. #ifdef URHO3D_OPENGL
  141. if (gapi == GAPI_OPENGL)
  142. return SetData_OGL(data);
  143. #endif
  144. #ifdef URHO3D_D3D11
  145. if (gapi == GAPI_D3D11)
  146. return SetData_D3D11(data);;
  147. #endif
  148. return {}; // Prevent warning
  149. }
  150. bool IndexBuffer::SetDataRange(const void* data, i32 start, i32 count, bool discard)
  151. {
  152. assert(start >= 0 && count >= 0);
  153. GAPI gapi = Graphics::GetGAPI();
  154. #ifdef URHO3D_OPENGL
  155. if (gapi == GAPI_OPENGL)
  156. return SetDataRange_OGL(data, start, count, discard);
  157. #endif
  158. #ifdef URHO3D_D3D11
  159. if (gapi == GAPI_D3D11)
  160. return SetDataRange_D3D11(data, start, count, discard);
  161. #endif
  162. return {}; // Prevent warning
  163. }
  164. void* IndexBuffer::Lock(i32 start, i32 count, bool discard)
  165. {
  166. assert(start >= 0 && count >= 0);
  167. GAPI gapi = Graphics::GetGAPI();
  168. #ifdef URHO3D_OPENGL
  169. if (gapi == GAPI_OPENGL)
  170. return Lock_OGL(start, count, discard);
  171. #endif
  172. #ifdef URHO3D_D3D11
  173. if (gapi == GAPI_D3D11)
  174. return Lock_D3D11(start, count, discard);
  175. #endif
  176. return {}; // Prevent warning
  177. }
  178. void IndexBuffer::Unlock()
  179. {
  180. GAPI gapi = Graphics::GetGAPI();
  181. #ifdef URHO3D_OPENGL
  182. if (gapi == GAPI_OPENGL)
  183. return Unlock_OGL();
  184. #endif
  185. #ifdef URHO3D_D3D11
  186. if (gapi == GAPI_D3D11)
  187. return Unlock_D3D11();
  188. #endif
  189. }
  190. bool IndexBuffer::Create()
  191. {
  192. GAPI gapi = Graphics::GetGAPI();
  193. #ifdef URHO3D_OPENGL
  194. if (gapi == GAPI_OPENGL)
  195. return Create_OGL();
  196. #endif
  197. #ifdef URHO3D_D3D11
  198. if (gapi == GAPI_D3D11)
  199. return Create_D3D11();
  200. #endif
  201. return {}; // Prevent warning
  202. }
  203. bool IndexBuffer::UpdateToGPU()
  204. {
  205. GAPI gapi = Graphics::GetGAPI();
  206. #ifdef URHO3D_OPENGL
  207. if (gapi == GAPI_OPENGL)
  208. return UpdateToGPU_OGL();
  209. #endif
  210. #ifdef URHO3D_D3D11
  211. if (gapi == GAPI_D3D11)
  212. return UpdateToGPU_D3D11();
  213. #endif
  214. return {}; // Prevent warning
  215. }
  216. void* IndexBuffer::MapBuffer(i32 start, i32 count, bool discard)
  217. {
  218. assert(start >= 0 && count >= 0);
  219. GAPI gapi = Graphics::GetGAPI();
  220. #ifdef URHO3D_OPENGL
  221. if (gapi == GAPI_OPENGL)
  222. return MapBuffer_OGL(start, count, discard);
  223. #endif
  224. #ifdef URHO3D_D3D11
  225. if (gapi == GAPI_D3D11)
  226. return MapBuffer_D3D11(start, count, discard);
  227. #endif
  228. return {}; // Prevent warning
  229. }
  230. void IndexBuffer::UnmapBuffer()
  231. {
  232. GAPI gapi = Graphics::GetGAPI();
  233. #ifdef URHO3D_OPENGL
  234. if (gapi == GAPI_OPENGL)
  235. return UnmapBuffer_OGL();
  236. #endif
  237. #ifdef URHO3D_D3D11
  238. if (gapi == GAPI_D3D11)
  239. return UnmapBuffer_D3D11();
  240. #endif
  241. }
  242. }