D3D9IndexBuffer.cpp 6.6 KB

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