IndexBuffer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "IndexBuffer.h"
  25. #include "Log.h"
  26. #include "Renderer.h"
  27. #include "RendererImpl.h"
  28. #include "DebugNew.h"
  29. IndexBuffer::IndexBuffer(Renderer* renderer, bool dynamic) :
  30. GPUObject(renderer),
  31. mIndexCount(0),
  32. mIndexSize(0),
  33. mLocked(false),
  34. mDataLost(false)
  35. {
  36. if (dynamic)
  37. {
  38. mPool = D3DPOOL_DEFAULT;
  39. mUsage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
  40. }
  41. else
  42. {
  43. mPool = D3DPOOL_MANAGED;
  44. mUsage = 0;
  45. }
  46. }
  47. IndexBuffer::~IndexBuffer()
  48. {
  49. release();
  50. }
  51. void IndexBuffer::onDeviceLost()
  52. {
  53. if (mPool == D3DPOOL_DEFAULT)
  54. release();
  55. }
  56. void IndexBuffer::onDeviceReset()
  57. {
  58. if (mPool == D3DPOOL_DEFAULT)
  59. {
  60. create();
  61. mDataLost = true;
  62. }
  63. }
  64. void IndexBuffer::setSize(unsigned indexCount, bool largeIndices)
  65. {
  66. setSize(indexCount, largeIndices ? sizeof(unsigned) : sizeof(unsigned short));
  67. }
  68. void IndexBuffer::setSize(unsigned indexCount, unsigned indexSize)
  69. {
  70. if ((indexSize != sizeof(unsigned)) && (indexSize != sizeof(unsigned short)))
  71. EXCEPTION("Index size not 2 or 4 bytes");
  72. if ((indexCount == mIndexCount) && (indexSize == mIndexSize))
  73. return;
  74. mIndexCount = indexCount;
  75. mIndexSize = indexSize;
  76. create();
  77. }
  78. void IndexBuffer::setData(const void* data)
  79. {
  80. void* hwData = lock(0, mIndexCount, LOCK_DISCARD);
  81. memcpy(hwData, data, mIndexCount * mIndexSize);
  82. unlock();
  83. }
  84. void IndexBuffer::setDataRange(const void* data, unsigned start, unsigned count)
  85. {
  86. if (!count)
  87. return;
  88. void* hwData = lock(start, count, LOCK_NORMAL);
  89. memcpy(hwData, data, count * mIndexSize);
  90. unlock();
  91. }
  92. void* IndexBuffer::lock(unsigned start, unsigned count, LockMode mode)
  93. {
  94. if ((!mObject) && (!mFallbackData))
  95. EXCEPTION("No index buffer created, can not lock");
  96. if (mLocked)
  97. EXCEPTION("Index buffer already locked");
  98. if ((!count) || (start + count > mIndexCount))
  99. EXCEPTION("Illegal range for locking index buffer");
  100. void* hwData;
  101. if (mObject)
  102. {
  103. DWORD flags = 0;
  104. if ((mode == LOCK_DISCARD) && (mUsage & D3DUSAGE_DYNAMIC))
  105. flags |= D3DLOCK_DISCARD;
  106. if (mode == LOCK_NOOVERWRITE)
  107. flags |= D3DLOCK_NOOVERWRITE;
  108. if (mode == LOCK_READONLY)
  109. flags = D3DLOCK_READONLY;
  110. if (FAILED(((IDirect3DIndexBuffer9*)mObject)->Lock(start * mIndexSize, count * mIndexSize, &hwData, flags)))
  111. EXCEPTION("Could not lock index buffer");
  112. }
  113. else
  114. hwData = mFallbackData.getPtr() + start * mIndexSize;
  115. mLocked = true;
  116. return hwData;
  117. }
  118. void IndexBuffer::unlock()
  119. {
  120. if (mLocked)
  121. {
  122. if (mObject)
  123. ((IDirect3DIndexBuffer9*)mObject)->Unlock();
  124. mLocked = false;
  125. }
  126. }
  127. void IndexBuffer::clearDataLost()
  128. {
  129. mDataLost = false;
  130. }
  131. bool IndexBuffer::isDynamic() const
  132. {
  133. return mPool == D3DPOOL_DEFAULT;
  134. }
  135. bool IndexBuffer::getUsedVertexRange(unsigned start, unsigned count, unsigned& minVertex, unsigned& vertexCount)
  136. {
  137. void* data = 0;
  138. try
  139. {
  140. data = lock(start, count, LOCK_READONLY);
  141. }
  142. catch (...)
  143. {
  144. return false;
  145. }
  146. minVertex = M_MAX_UNSIGNED;
  147. unsigned maxVertex = 0;
  148. if (mIndexSize == sizeof(unsigned))
  149. {
  150. unsigned* indices = (unsigned*)data;
  151. for (unsigned i = 0; i < count; ++i)
  152. {
  153. if (indices[i] < minVertex)
  154. minVertex = indices[i];
  155. if (indices[i] > maxVertex)
  156. maxVertex = indices[i];
  157. }
  158. }
  159. else
  160. {
  161. unsigned short* indices = (unsigned short*)data;
  162. for (unsigned i = 0; i < count; ++i)
  163. {
  164. if (indices[i] < minVertex)
  165. minVertex = indices[i];
  166. if (indices[i] > maxVertex)
  167. maxVertex = indices[i];
  168. }
  169. }
  170. vertexCount = maxVertex - minVertex + 1;
  171. unlock();
  172. return true;
  173. }
  174. void IndexBuffer::create()
  175. {
  176. release();
  177. if (!mIndexCount)
  178. return;
  179. if (mRenderer)
  180. {
  181. if (FAILED(mRenderer->getImpl()->getDevice()->CreateIndexBuffer(
  182. mIndexCount * mIndexSize,
  183. mUsage,
  184. mIndexSize == sizeof(unsigned) ? D3DFMT_INDEX32 : D3DFMT_INDEX16,
  185. (D3DPOOL)mPool,
  186. (IDirect3DIndexBuffer9**)&mObject,
  187. 0)))
  188. EXCEPTION("Could not create index buffer");
  189. }
  190. else
  191. mFallbackData = new unsigned char[mIndexCount * mIndexSize];
  192. }
  193. void IndexBuffer::release()
  194. {
  195. if (mObject)
  196. {
  197. if (!mRenderer)
  198. {
  199. LOGWARNING("Renderer has expired, skipping release of IndexBuffer");
  200. return;
  201. }
  202. if (mRenderer->getIndexBuffer() == this)
  203. mRenderer->setIndexBuffer(0);
  204. ((IDirect3DIndexBuffer9*)mObject)->Release();
  205. mObject = 0;
  206. }
  207. mFallbackData.reset();
  208. }