D3D9IndexBuffer.cpp 6.3 KB

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