D3D9IndexBuffer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. locked_(false),
  39. dataLost_(false)
  40. {
  41. }
  42. IndexBuffer::~IndexBuffer()
  43. {
  44. Release();
  45. }
  46. void IndexBuffer::OnDeviceLost()
  47. {
  48. if (pool_ == D3DPOOL_DEFAULT)
  49. Release();
  50. }
  51. void IndexBuffer::OnDeviceReset()
  52. {
  53. if (pool_ == D3DPOOL_DEFAULT)
  54. {
  55. Create();
  56. dataLost_ = true;
  57. }
  58. }
  59. void IndexBuffer::Release()
  60. {
  61. if (object_)
  62. {
  63. if (!graphics_)
  64. return;
  65. Unlock();
  66. if (graphics_->GetIndexBuffer() == this)
  67. graphics_->SetIndexBuffer(0);
  68. ((IDirect3DIndexBuffer9*)object_)->Release();
  69. object_ = 0;
  70. }
  71. fallbackData_.Reset();
  72. }
  73. bool IndexBuffer::SetSize(unsigned indexCount, bool largeIndices, bool dynamic)
  74. {
  75. if (dynamic)
  76. {
  77. pool_ = D3DPOOL_DEFAULT;
  78. usage_ = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
  79. }
  80. else
  81. {
  82. pool_ = D3DPOOL_MANAGED;
  83. usage_ = 0;
  84. }
  85. indexCount_ = indexCount;
  86. indexSize_ = largeIndices ? sizeof(unsigned) : sizeof(unsigned short);
  87. return Create();
  88. }
  89. bool IndexBuffer::SetData(const void* data)
  90. {
  91. void* hwData = Lock(0, indexCount_, LOCK_DISCARD);
  92. if (!hwData)
  93. return false;
  94. memcpy(hwData, data, indexCount_ * indexSize_);
  95. Unlock();
  96. return true;
  97. }
  98. bool IndexBuffer::SetDataRange(const void* data, unsigned start, unsigned count)
  99. {
  100. if (!count)
  101. return true;
  102. void* hwData = Lock(start, count, LOCK_NORMAL);
  103. if (!hwData)
  104. return false;
  105. memcpy(hwData, data, count * indexSize_);
  106. Unlock();
  107. return true;
  108. }
  109. void* IndexBuffer::Lock(unsigned start, unsigned count, LockMode mode)
  110. {
  111. if (!object_ && !fallbackData_)
  112. return 0;
  113. if (locked_)
  114. {
  115. LOGERROR("Index buffer already locked");
  116. return 0;
  117. }
  118. if (!count || start + count > indexCount_)
  119. {
  120. LOGERROR("Illegal range for locking index buffer");
  121. return 0;
  122. }
  123. void* hwData = 0;
  124. if (object_)
  125. {
  126. DWORD flags = 0;
  127. if (mode == LOCK_DISCARD && usage_ & D3DUSAGE_DYNAMIC)
  128. flags = D3DLOCK_DISCARD;
  129. if (mode == LOCK_NOOVERWRITE && usage_ & D3DUSAGE_DYNAMIC)
  130. flags = D3DLOCK_NOOVERWRITE;
  131. if (mode == LOCK_READONLY)
  132. flags = D3DLOCK_READONLY;
  133. if (FAILED(((IDirect3DIndexBuffer9*)object_)->Lock(start * indexSize_, count * indexSize_, &hwData, flags)))
  134. {
  135. LOGERROR("Could not lock index buffer");
  136. return 0;
  137. }
  138. }
  139. else
  140. hwData = fallbackData_.Get() + start * indexSize_;
  141. locked_ = true;
  142. return hwData;
  143. }
  144. void IndexBuffer::Unlock()
  145. {
  146. if (locked_)
  147. {
  148. if (object_)
  149. ((IDirect3DIndexBuffer9*)object_)->Unlock();
  150. locked_ = false;
  151. }
  152. }
  153. void IndexBuffer::ClearDataLost()
  154. {
  155. dataLost_ = false;
  156. }
  157. bool IndexBuffer::IsDynamic() const
  158. {
  159. return pool_ == D3DPOOL_DEFAULT;
  160. }
  161. bool IndexBuffer::GetUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  162. {
  163. void* data = Lock(start, count, LOCK_READONLY);
  164. if (!data)
  165. return false;
  166. minVertex = M_MAX_UNSIGNED;
  167. unsigned maxVertex = 0;
  168. if (indexSize_ == sizeof(unsigned))
  169. {
  170. unsigned* indices = (unsigned*)data;
  171. for (unsigned i = 0; i < count; ++i)
  172. {
  173. if (indices[i] < minVertex)
  174. minVertex = indices[i];
  175. if (indices[i] > maxVertex)
  176. maxVertex = indices[i];
  177. }
  178. }
  179. else
  180. {
  181. unsigned short* indices = (unsigned short*)data;
  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. vertexCount = maxVertex - minVertex + 1;
  191. Unlock();
  192. return true;
  193. }
  194. bool IndexBuffer::Create()
  195. {
  196. Release();
  197. if (!indexCount_)
  198. return true;
  199. if (graphics_)
  200. {
  201. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  202. if (!device || FAILED(device->CreateIndexBuffer(
  203. indexCount_ * indexSize_,
  204. usage_,
  205. indexSize_ == sizeof(unsigned) ? D3DFMT_INDEX32 : D3DFMT_INDEX16,
  206. (D3DPOOL)pool_,
  207. (IDirect3DIndexBuffer9**)&object_,
  208. 0)))
  209. {
  210. LOGERROR("Could not create index buffer");
  211. return false;
  212. }
  213. }
  214. else
  215. fallbackData_ = new unsigned char[indexCount_ * indexSize_];
  216. return true;
  217. }