CmD3D9IndexBuffer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  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. */
  24. #include "CmD3D9IndexBuffer.h"
  25. #include "CmD3D9Mappings.h"
  26. #include "CmException.h"
  27. #include "CmD3D9HardwareBufferManager.h"
  28. #include "CmD3D9RenderSystem.h"
  29. #include "CmD3D9Device.h"
  30. #include "CmD3D9ResourceManager.h"
  31. namespace CamelotFramework {
  32. //---------------------------------------------------------------------
  33. D3D9IndexBuffer::D3D9IndexBuffer(HardwareBufferManager* mgr, IndexBuffer::IndexType idxType,
  34. UINT32 numIndexes, GpuBufferUsage usage,
  35. bool useSystemMemory)
  36. : IndexBuffer(mgr, idxType, numIndexes, usage, useSystemMemory)
  37. {
  38. }
  39. //---------------------------------------------------------------------
  40. D3D9IndexBuffer::~D3D9IndexBuffer()
  41. { }
  42. //---------------------------------------------------------------------
  43. void D3D9IndexBuffer::initialize_internal()
  44. {
  45. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  46. D3DPOOL eResourcePool = mSystemMemory? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT;
  47. // Set the desired memory pool.
  48. mBufferDesc.Pool = eResourcePool;
  49. // Allocate the system memory buffer.
  50. mSystemMemoryBuffer = (char*) cm_alloc<ScratchAlloc>(getSizeInBytes());
  51. memset(mSystemMemoryBuffer, 0, getSizeInBytes());
  52. // Case we have to create this buffer resource on loading.
  53. if (D3D9RenderSystem::getResourceManager()->getCreationPolicy() == RCP_CREATE_ON_ALL_DEVICES)
  54. {
  55. for (UINT32 i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  56. {
  57. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  58. createBuffer(d3d9Device, mBufferDesc.Pool);
  59. }
  60. }
  61. IndexBuffer::initialize_internal();
  62. }
  63. //---------------------------------------------------------------------
  64. void D3D9IndexBuffer::destroy_internal()
  65. {
  66. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  67. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  68. while (it != mMapDeviceToBufferResources.end())
  69. {
  70. SAFE_RELEASE(it->second->mBuffer);
  71. if(it->second != nullptr)
  72. cm_delete<PoolAlloc>(it->second);
  73. ++it;
  74. }
  75. mMapDeviceToBufferResources.clear();
  76. if(mSystemMemoryBuffer != nullptr)
  77. cm_free<ScratchAlloc>(mSystemMemoryBuffer);
  78. IndexBuffer::destroy_internal();
  79. }
  80. //---------------------------------------------------------------------
  81. void* D3D9IndexBuffer::lockImpl(UINT32 offset,
  82. UINT32 length, GpuLockOptions options)
  83. {
  84. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  85. if (options != GBL_READ_ONLY)
  86. {
  87. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  88. while (it != mMapDeviceToBufferResources.end())
  89. {
  90. BufferResources* bufferResources = it->second;
  91. bufferResources->mOutOfDate = true;
  92. if(bufferResources->mLockLength > 0)
  93. {
  94. UINT32 highPoint = std::max( offset + length,
  95. bufferResources->mLockOffset + bufferResources->mLockLength );
  96. bufferResources->mLockOffset = std::min( bufferResources->mLockOffset, offset );
  97. bufferResources->mLockLength = highPoint - bufferResources->mLockOffset;
  98. }
  99. else
  100. {
  101. if (offset < bufferResources->mLockOffset)
  102. bufferResources->mLockOffset = offset;
  103. if (length > bufferResources->mLockLength)
  104. bufferResources->mLockLength = length;
  105. }
  106. if (bufferResources->mLockOptions != GBL_WRITE_ONLY_DISCARD)
  107. bufferResources->mLockOptions = options;
  108. ++it;
  109. }
  110. }
  111. return mSystemMemoryBuffer + offset;
  112. }
  113. //---------------------------------------------------------------------
  114. void D3D9IndexBuffer::unlockImpl(void)
  115. {
  116. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  117. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  118. // TODO PORT - nextFrameNumber will always be 0 for now. This has potential for errors, so make sure to add a frame counting method before port is done
  119. //UINT32 nextFrameNumber = Root::getSingleton().getNextFrameNumber();
  120. UINT32 nextFrameNumber = 0;
  121. while (it != mMapDeviceToBufferResources.end())
  122. {
  123. BufferResources* bufferResources = it->second;
  124. if (bufferResources->mOutOfDate &&
  125. bufferResources->mBuffer != NULL &&
  126. nextFrameNumber - bufferResources->mLastUsedFrame <= 1)
  127. updateBufferResources(mSystemMemoryBuffer, bufferResources);
  128. ++it;
  129. }
  130. }
  131. //---------------------------------------------------------------------
  132. void D3D9IndexBuffer::readData(UINT32 offset, UINT32 length,
  133. void* pDest)
  134. {
  135. // There is no functional interface in D3D, just do via manual
  136. // lock, copy & unlock
  137. void* pSrc = this->lock(offset, length, GBL_READ_ONLY);
  138. memcpy(pDest, pSrc, length);
  139. this->unlock();
  140. }
  141. //---------------------------------------------------------------------
  142. void D3D9IndexBuffer::writeData(UINT32 offset, UINT32 length,
  143. const void* pSource, BufferWriteType writeFlags)
  144. {
  145. GpuLockOptions lockOption = GBL_WRITE_ONLY;
  146. if(writeFlags == BufferWriteType::Discard)
  147. lockOption = GBL_WRITE_ONLY_DISCARD;
  148. else if(writeFlags == BufferWriteType::NoOverwrite)
  149. lockOption = GBL_WRITE_ONLY_NO_OVERWRITE;
  150. // There is no functional interface in D3D, just do via manual
  151. // lock, copy & unlock
  152. void* pDst = this->lock(offset, length, lockOption);
  153. memcpy(pDst, pSource, length);
  154. this->unlock();
  155. }
  156. //---------------------------------------------------------------------
  157. void D3D9IndexBuffer::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  158. {
  159. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  160. if (D3D9RenderSystem::getResourceManager()->getCreationPolicy() == RCP_CREATE_ON_ALL_DEVICES)
  161. createBuffer(d3d9Device, mBufferDesc.Pool);
  162. }
  163. //---------------------------------------------------------------------
  164. void D3D9IndexBuffer::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  165. {
  166. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  167. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.find(d3d9Device);
  168. if (it != mMapDeviceToBufferResources.end())
  169. {
  170. SAFE_RELEASE(it->second->mBuffer);
  171. if(it->second != nullptr)
  172. cm_delete<PoolAlloc>(it->second);
  173. mMapDeviceToBufferResources.erase(it);
  174. }
  175. }
  176. //---------------------------------------------------------------------
  177. void D3D9IndexBuffer::notifyOnDeviceLost(IDirect3DDevice9* d3d9Device)
  178. {
  179. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  180. if (mBufferDesc.Pool == D3DPOOL_DEFAULT)
  181. {
  182. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.find(d3d9Device);
  183. if (it != mMapDeviceToBufferResources.end())
  184. {
  185. SAFE_RELEASE(it->second->mBuffer);
  186. }
  187. }
  188. }
  189. //---------------------------------------------------------------------
  190. void D3D9IndexBuffer::notifyOnDeviceReset(IDirect3DDevice9* d3d9Device)
  191. {
  192. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  193. if (D3D9RenderSystem::getResourceManager()->getCreationPolicy() == RCP_CREATE_ON_ALL_DEVICES)
  194. createBuffer(d3d9Device, mBufferDesc.Pool);
  195. }
  196. //---------------------------------------------------------------------
  197. void D3D9IndexBuffer::createBuffer(IDirect3DDevice9* d3d9Device, D3DPOOL ePool)
  198. {
  199. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  200. BufferResources* bufferResources;
  201. HRESULT hr;
  202. DeviceToBufferResourcesIterator it;
  203. // Find the vertex buffer of this device.
  204. it = mMapDeviceToBufferResources.find(d3d9Device);
  205. if (it != mMapDeviceToBufferResources.end())
  206. {
  207. bufferResources = it->second;
  208. SAFE_RELEASE(bufferResources->mBuffer);
  209. }
  210. else
  211. {
  212. bufferResources = cm_new<BufferResources, PoolAlloc>();
  213. mMapDeviceToBufferResources[d3d9Device] = bufferResources;
  214. }
  215. bufferResources->mBuffer = NULL;
  216. bufferResources->mOutOfDate = true;
  217. bufferResources->mLockOffset = 0;
  218. bufferResources->mLockLength = getSizeInBytes();
  219. bufferResources->mLockOptions = GBL_READ_WRITE;
  220. // TODO PORT - I don't know current frame number. Once I add a method for counting frames call it here
  221. //bufferResources->mLastUsedFrame = Root::getSingleton().getNextFrameNumber();
  222. bufferResources->mLastUsedFrame = 0;
  223. // Create the Index buffer
  224. hr = d3d9Device->CreateIndexBuffer(
  225. static_cast<UINT>(mSizeInBytes),
  226. D3D9Mappings::get(mUsage),
  227. D3D9Mappings::get(mIndexType),
  228. ePool,
  229. &bufferResources->mBuffer,
  230. NULL
  231. );
  232. if (FAILED(hr))
  233. {
  234. String msg = DXGetErrorDescription(hr);
  235. CM_EXCEPT(RenderingAPIException, "Cannot create D3D9 Index buffer: " + msg);
  236. }
  237. hr = bufferResources->mBuffer->GetDesc(&mBufferDesc);
  238. if (FAILED(hr))
  239. {
  240. String msg = DXGetErrorDescription(hr);
  241. CM_EXCEPT(RenderingAPIException, "Cannot get D3D9 Index buffer desc: " + msg);
  242. }
  243. }
  244. //---------------------------------------------------------------------
  245. IDirect3DIndexBuffer9* D3D9IndexBuffer::getD3DIndexBuffer(void)
  246. {
  247. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  248. DeviceToBufferResourcesIterator it;
  249. // Find the index buffer of this device.
  250. it = mMapDeviceToBufferResources.find(d3d9Device);
  251. // Case index buffer was not found for the current device -> create it.
  252. if (it == mMapDeviceToBufferResources.end() || it->second->mBuffer == NULL)
  253. {
  254. createBuffer(d3d9Device, mBufferDesc.Pool);
  255. it = mMapDeviceToBufferResources.find(d3d9Device);
  256. }
  257. if (it->second->mOutOfDate)
  258. updateBufferResources(mSystemMemoryBuffer, it->second);
  259. // TODO PORT - I don't know current frame number. Once I add a method for counting frames call it here
  260. //it->second->mLastUsedFrame = Root::getSingleton().getNextFrameNumber();
  261. it->second->mLastUsedFrame = 0;
  262. return it->second->mBuffer;
  263. }
  264. //---------------------------------------------------------------------
  265. bool D3D9IndexBuffer::updateBufferResources(const char* systemMemoryBuffer,
  266. BufferResources* bufferResources)
  267. {
  268. assert(bufferResources != NULL);
  269. assert(bufferResources->mBuffer != NULL);
  270. assert(bufferResources->mOutOfDate);
  271. void* dstBytes;
  272. HRESULT hr;
  273. // Lock the buffer.
  274. hr = bufferResources->mBuffer->Lock(
  275. static_cast<UINT>(bufferResources->mLockOffset),
  276. static_cast<UINT>(bufferResources->mLockLength),
  277. &dstBytes,
  278. D3D9Mappings::get(bufferResources->mLockOptions, mUsage));
  279. if (FAILED(hr))
  280. {
  281. String msg = DXGetErrorDescription(hr);
  282. CM_EXCEPT(RenderingAPIException, "Cannot lock D3D9 vertex buffer: " + msg);
  283. }
  284. memcpy(dstBytes, systemMemoryBuffer + bufferResources->mLockOffset, bufferResources->mLockLength);
  285. // Unlock the buffer.
  286. hr = bufferResources->mBuffer->Unlock();
  287. if (FAILED(hr))
  288. {
  289. String msg = DXGetErrorDescription(hr);
  290. CM_EXCEPT(RenderingAPIException, "Cannot unlock D3D9 vertex buffer: " + msg);
  291. }
  292. bufferResources->mOutOfDate = false;
  293. bufferResources->mLockOffset = mSizeInBytes;
  294. bufferResources->mLockLength = 0;
  295. bufferResources->mLockOptions = GBL_READ_WRITE;
  296. return true;
  297. }
  298. }