D3D9IndexBuffer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "Graphics.h"
  26. #include "GraphicsImpl.h"
  27. #include "IndexBuffer.h"
  28. #include "Log.h"
  29. #include "DebugNew.h"
  30. OBJECTTYPESTATIC(IndexBuffer);
  31. IndexBuffer::IndexBuffer(Context* context) :
  32. Object(context),
  33. GPUObject(GetSubsystem<Graphics>()),
  34. pool_(D3DPOOL_MANAGED),
  35. usage_(0),
  36. indexCount_(0),
  37. indexSize_(0),
  38. shadowed_(false),
  39. dataLost_(false)
  40. {
  41. }
  42. IndexBuffer::~IndexBuffer()
  43. {
  44. Release();
  45. }
  46. void IndexBuffer::OnDeviceLost()
  47. {
  48. if (pool_ == D3DPOOL_DEFAULT)
  49. {
  50. Release();
  51. dataLost_ = true;
  52. }
  53. }
  54. void IndexBuffer::OnDeviceReset()
  55. {
  56. if (pool_ == D3DPOOL_DEFAULT)
  57. {
  58. Create();
  59. if (UpdateToGPU())
  60. dataLost_ = false;
  61. }
  62. }
  63. void IndexBuffer::Release()
  64. {
  65. if (object_)
  66. {
  67. if (!graphics_)
  68. return;
  69. if (graphics_->GetIndexBuffer() == this)
  70. graphics_->SetIndexBuffer(0);
  71. ((IDirect3DIndexBuffer9*)object_)->Release();
  72. object_ = 0;
  73. }
  74. }
  75. void IndexBuffer::SetShadowed(bool enable)
  76. {
  77. // If no graphics subsystem, can not disable shadowing
  78. if (!graphics_)
  79. enable = true;
  80. if (enable != shadowed_)
  81. {
  82. if (enable && indexSize_ && indexCount_)
  83. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  84. else
  85. shadowData_.Reset();
  86. shadowed_ = enable;
  87. }
  88. }
  89. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  90. {
  91. if (dynamic)
  92. {
  93. pool_ = D3DPOOL_DEFAULT;
  94. usage_ = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
  95. }
  96. else
  97. {
  98. pool_ = D3DPOOL_MANAGED;
  99. usage_ = 0;
  100. }
  101. indexCount_ = indexCount;
  102. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  103. if (shadowed_ && indexCount_ && indexSize_)
  104. shadowData_ = new unsigned char[indexCount_ * indexSize_];
  105. else
  106. shadowData_.Reset();
  107. return Create();
  108. }
  109. bool IndexBuffer::SetData(const void* data)
  110. {
  111. if (!data)
  112. {
  113. LOGERROR("Null pointer for index buffer data");
  114. return false;
  115. }
  116. if (object_)
  117. {
  118. void* hwData = Lock(0, indexCount_, true);
  119. if (hwData)
  120. {
  121. memcpy(hwData, data, indexCount_ * indexSize_);
  122. Unlock();
  123. }
  124. }
  125. if (shadowData_ && data != shadowData_.Get())
  126. memcpy(shadowData_.Get(), data, indexCount_ * indexSize_);
  127. return true;
  128. }
  129. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count, bool discard)
  130. {
  131. if (start == 0 && count == indexCount_)
  132. return SetData(data);
  133. if (!data)
  134. {
  135. LOGERROR("Null pointer for index buffer data");
  136. return false;
  137. }
  138. if (start + count > indexCount_)
  139. {
  140. LOGERROR("Illegal range for setting new index buffer data");
  141. return false;
  142. }
  143. if (!count)
  144. return true;
  145. if (object_)
  146. {
  147. void* hwData = Lock(start, count, discard);
  148. if (hwData)
  149. {
  150. memcpy(hwData, data, count * indexSize_);
  151. Unlock();
  152. }
  153. }
  154. if (shadowData_ && shadowData_.Get() + start * indexSize_ != data)
  155. memcpy(shadowData_.Get() + start * indexSize_, data, count * indexSize_);
  156. return true;
  157. }
  158. bool IndexBuffer::UpdateToGPU()
  159. {
  160. if (object_ && shadowData_)
  161. return SetData(shadowData_.Get());
  162. else
  163. return false;
  164. }
  165. void IndexBuffer::ClearDataLost()
  166. {
  167. dataLost_ = false;
  168. }
  169. bool IndexBuffer::IsDynamic() const
  170. {
  171. return pool_ == D3DPOOL_DEFAULT;
  172. }
  173. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  174. {
  175. if (!shadowData_)
  176. return false;
  177. minVertex = M_MAX_UNSIGNED;
  178. unsigned maxVertex = 0;
  179. if (indexSize_ == sizeof(unsigned))
  180. {
  181. unsigned* indices = (unsigned*)shadowData_.Get();
  182. for (unsigned i = 0; i < count; ++i)
  183. {
  184. if (indices[i] < minVertex)
  185. minVertex = indices[i];
  186. if (indices[i] > maxVertex)
  187. maxVertex = indices[i];
  188. }
  189. }
  190. else
  191. {
  192. unsigned short* indices = (unsigned short*)shadowData_.Get();
  193. for (unsigned i = 0; i < count; ++i)
  194. {
  195. if (indices[i] < minVertex)
  196. minVertex = indices[i];
  197. if (indices[i] > maxVertex)
  198. maxVertex = indices[i];
  199. }
  200. }
  201. vertexCount = maxVertex - minVertex + 1;
  202. return true;
  203. }
  204. bool IndexBuffer::Create()
  205. {
  206. Release();
  207. if (!indexCount_)
  208. return true;
  209. if (graphics_)
  210. {
  211. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  212. if (!device || FAILED(device->CreateIndexBuffer(
  213. indexCount_ * indexSize_,
  214. usage_,
  215. indexSize_ == sizeof(unsigned) ? D3DFMT_INDEX32 : D3DFMT_INDEX16,
  216. (D3DPOOL)pool_,
  217. (IDirect3DIndexBuffer9**)&object_,
  218. 0)))
  219. {
  220. LOGERROR("Could not create index buffer");
  221. return false;
  222. }
  223. }
  224. return true;
  225. }
  226. void* IndexBuffer::Lock(unsigned start, unsigned count, bool discard)
  227. {
  228. void* hwData = 0;
  229. if (object_)
  230. {
  231. DWORD flags = 0;
  232. if (discard && usage_ & D3DUSAGE_DYNAMIC)
  233. flags = D3DLOCK_DISCARD;
  234. if (FAILED(((IDirect3DIndexBuffer9*)object_)->Lock(start * indexSize_, count * indexSize_, &hwData, flags)))
  235. {
  236. LOGERROR("Could not lock index buffer");
  237. return 0;
  238. }
  239. }
  240. return hwData;
  241. }
  242. void IndexBuffer::Unlock()
  243. {
  244. if (object_)
  245. ((IDirect3DIndexBuffer9*)object_)->Unlock();
  246. }