CmD3D9PixelBuffer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "CmD3D9PixelBuffer.h"
  25. #include "CmD3D9Texture.h"
  26. #include "CmD3D9Mappings.h"
  27. #include "CmException.h"
  28. #include "CmBitwise.h"
  29. #include "CmRenderSystem.h"
  30. namespace CamelotFramework
  31. {
  32. CM_STATIC_MUTEX_INSTANCE(D3D9PixelBuffer::msDeviceAccessMutex)
  33. //-----------------------------------------------------------------------------
  34. D3D9PixelBuffer::D3D9PixelBuffer(GpuBufferUsage usage,
  35. D3D9Texture* ownerTexture):
  36. PixelBuffer(0, 0, 0, PF_UNKNOWN, usage, false),
  37. mDoMipmapGen(0), mHWMipmaps(0), mOwnerTexture(ownerTexture)
  38. {
  39. }
  40. D3D9PixelBuffer::~D3D9PixelBuffer()
  41. {
  42. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  43. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  44. while (it != mMapDeviceToBufferResources.end())
  45. {
  46. SAFE_RELEASE(it->second->surface);
  47. SAFE_RELEASE(it->second->volume);
  48. if(it->second != nullptr)
  49. cm_delete<PoolAlloc>(it->second);
  50. DeviceToBufferResourcesIterator deadi = it++;
  51. mMapDeviceToBufferResources.erase(deadi);
  52. }
  53. }
  54. //-----------------------------------------------------------------------------
  55. void D3D9PixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DSurface9 *surface,
  56. bool writeGamma, UINT32 fsaa, const String& srcName,
  57. IDirect3DBaseTexture9 *mipTex)
  58. {
  59. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  60. BufferResources* bufferResources = getBufferResources(dev);
  61. bool isNewBuffer = false;
  62. if (bufferResources == NULL)
  63. {
  64. bufferResources = createBufferResources();
  65. mMapDeviceToBufferResources[dev] = bufferResources;
  66. isNewBuffer = true;
  67. }
  68. bufferResources->mipTex = mipTex;
  69. bufferResources->surface = surface;
  70. bufferResources->surface->AddRef();
  71. D3DSURFACE_DESC desc;
  72. if(surface->GetDesc(&desc) != D3D_OK)
  73. CM_EXCEPT(RenderingAPIException, "Could not get surface information");
  74. mWidth = desc.Width;
  75. mHeight = desc.Height;
  76. mDepth = 1;
  77. mFormat = D3D9Mappings::_getPF(desc.Format);
  78. // Default
  79. mRowPitch = mWidth;
  80. mSlicePitch = mHeight*mWidth;
  81. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  82. }
  83. //-----------------------------------------------------------------------------
  84. void D3D9PixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DVolume9 *volume, IDirect3DBaseTexture9 *mipTex)
  85. {
  86. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  87. BufferResources* bufferResources = getBufferResources(dev);
  88. bool isNewBuffer = false;
  89. if (bufferResources == NULL)
  90. {
  91. bufferResources = createBufferResources();
  92. mMapDeviceToBufferResources[dev] = bufferResources;
  93. isNewBuffer = true;
  94. }
  95. bufferResources->mipTex = mipTex;
  96. bufferResources->volume = volume;
  97. bufferResources->volume->AddRef();
  98. D3DVOLUME_DESC desc;
  99. if(volume->GetDesc(&desc) != D3D_OK)
  100. CM_EXCEPT(RenderingAPIException, "Could not get volume information");
  101. mWidth = desc.Width;
  102. mHeight = desc.Height;
  103. mDepth = desc.Depth;
  104. mFormat = D3D9Mappings::_getPF(desc.Format);
  105. // Default
  106. mRowPitch = mWidth;
  107. mSlicePitch = mHeight*mWidth;
  108. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  109. }
  110. //-----------------------------------------------------------------------------
  111. D3D9PixelBuffer::BufferResources* D3D9PixelBuffer::getBufferResources(IDirect3DDevice9* d3d9Device)
  112. {
  113. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.find(d3d9Device);
  114. if (it != mMapDeviceToBufferResources.end())
  115. return it->second;
  116. return NULL;
  117. }
  118. //-----------------------------------------------------------------------------
  119. D3D9PixelBuffer::BufferResources* D3D9PixelBuffer::createBufferResources()
  120. {
  121. BufferResources* newResources = cm_new<BufferResources, PoolAlloc>();
  122. memset(newResources, 0, sizeof(BufferResources));
  123. return newResources;
  124. }
  125. //-----------------------------------------------------------------------------
  126. void D3D9PixelBuffer::destroyBufferResources(IDirect3DDevice9* d3d9Device)
  127. {
  128. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  129. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.find(d3d9Device);
  130. if (it != mMapDeviceToBufferResources.end())
  131. {
  132. SAFE_RELEASE(it->second->surface);
  133. SAFE_RELEASE(it->second->volume);
  134. if(it->second != nullptr)
  135. cm_delete<PoolAlloc>(it->second);
  136. mMapDeviceToBufferResources.erase(it);
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. void D3D9PixelBuffer::lockDeviceAccess()
  141. {
  142. D3D9_DEVICE_ACCESS_LOCK;
  143. }
  144. //-----------------------------------------------------------------------------
  145. void D3D9PixelBuffer::unlockDeviceAccess()
  146. {
  147. D3D9_DEVICE_ACCESS_UNLOCK;
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Util functions to convert a D3D locked box to a pixel box
  151. void fromD3DLock(PixelData &rval, const D3DLOCKED_RECT &lrect)
  152. {
  153. UINT32 bpp = PixelUtil::getNumElemBytes(rval.getFormat());
  154. if (bpp != 0)
  155. {
  156. rval.setRowPitch(lrect.Pitch / bpp);
  157. rval.setSlicePitch(rval.getRowPitch() * rval.getHeight());
  158. assert((lrect.Pitch % bpp)==0);
  159. }
  160. else if (PixelUtil::isCompressed(rval.getFormat()))
  161. {
  162. rval.setRowPitch(rval.getWidth());
  163. rval.setSlicePitch(rval.getWidth() * rval.getHeight());
  164. }
  165. else
  166. {
  167. CM_EXCEPT(InvalidParametersException, "Invalid pixel format");
  168. }
  169. rval.setExternalBuffer((UINT8*)lrect.pBits);
  170. }
  171. void fromD3DLock(PixelData &rval, const D3DLOCKED_BOX &lbox)
  172. {
  173. UINT32 bpp = PixelUtil::getNumElemBytes(rval.getFormat());
  174. if (bpp != 0)
  175. {
  176. rval.setRowPitch(lbox.RowPitch / bpp);
  177. rval.setSlicePitch(lbox.SlicePitch / bpp);
  178. assert((lbox.RowPitch % bpp)==0);
  179. assert((lbox.SlicePitch % bpp)==0);
  180. }
  181. else if (PixelUtil::isCompressed(rval.getFormat()))
  182. {
  183. rval.setRowPitch(rval.getWidth());
  184. rval.setSlicePitch(rval.getWidth() * rval.getHeight());
  185. }
  186. else
  187. {
  188. CM_EXCEPT(InvalidParametersException, "Invalid pixel format");
  189. }
  190. rval.setExternalBuffer((UINT8*)lbox.pBits);
  191. }
  192. // Convert Ogre integer Box to D3D rectangle
  193. RECT toD3DRECT(const PixelVolume &lockBox)
  194. {
  195. RECT prect;
  196. assert(lockBox.getDepth() == 1);
  197. prect.left = static_cast<LONG>(lockBox.left);
  198. prect.right = static_cast<LONG>(lockBox.right);
  199. prect.top = static_cast<LONG>(lockBox.top);
  200. prect.bottom = static_cast<LONG>(lockBox.bottom);
  201. return prect;
  202. }
  203. // Convert Ogre integer Box to D3D box
  204. D3DBOX toD3DBOX(const PixelVolume &lockBox)
  205. {
  206. D3DBOX pbox;
  207. pbox.Left = static_cast<UINT>(lockBox.left);
  208. pbox.Right = static_cast<UINT>(lockBox.right);
  209. pbox.Top = static_cast<UINT>(lockBox.top);
  210. pbox.Bottom = static_cast<UINT>(lockBox.bottom);
  211. pbox.Front = static_cast<UINT>(lockBox.front);
  212. pbox.Back = static_cast<UINT>(lockBox.back);
  213. return pbox;
  214. }
  215. // Convert Ogre pixelbox extent to D3D rectangle
  216. RECT toD3DRECTExtent(const PixelData &lockBox)
  217. {
  218. RECT prect;
  219. assert(lockBox.getDepth() == 1);
  220. prect.left = 0;
  221. prect.right = static_cast<LONG>(lockBox.getWidth());
  222. prect.top = 0;
  223. prect.bottom = static_cast<LONG>(lockBox.getHeight());
  224. return prect;
  225. }
  226. // Convert Ogre pixelbox extent to D3D box
  227. D3DBOX toD3DBOXExtent(const PixelData &lockBox)
  228. {
  229. D3DBOX pbox;
  230. pbox.Left = 0;
  231. pbox.Right = static_cast<UINT>(lockBox.getWidth());
  232. pbox.Top = 0;
  233. pbox.Bottom = static_cast<UINT>(lockBox.getHeight());
  234. pbox.Front = 0;
  235. pbox.Back = static_cast<UINT>(lockBox.getDepth());
  236. return pbox;
  237. }
  238. //-----------------------------------------------------------------------------
  239. PixelData D3D9PixelBuffer::lockImpl(const PixelVolume lockBox, GpuLockOptions options)
  240. {
  241. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  242. // Set locking flags according to options
  243. DWORD flags = 0;
  244. switch(options)
  245. {
  246. case GBL_WRITE_ONLY_DISCARD:
  247. // D3D only likes D3DLOCK_DISCARD if you created the texture with D3DUSAGE_DYNAMIC
  248. // debug runtime flags this up, could cause problems on some drivers
  249. if (mUsage & GBU_DYNAMIC)
  250. flags |= D3DLOCK_DISCARD;
  251. break;
  252. case GBL_READ_ONLY:
  253. flags |= D3DLOCK_READONLY;
  254. break;
  255. default:
  256. break;
  257. };
  258. if (mMapDeviceToBufferResources.size() == 0)
  259. {
  260. CM_EXCEPT(RenderingAPIException, "There are no resources attached to this pixel buffer !!");
  261. }
  262. mLockedBox = lockBox;
  263. mLockFlags = flags;
  264. BufferResources* bufferResources = mMapDeviceToBufferResources.begin()->second;
  265. // Lock the source buffer.
  266. return lockBuffer(bufferResources, lockBox, flags);
  267. }
  268. //-----------------------------------------------------------------------------
  269. CamelotFramework::PixelData D3D9PixelBuffer::lockBuffer(BufferResources* bufferResources,
  270. const PixelVolume &lockBox,
  271. DWORD flags)
  272. {
  273. // Set extents and format
  274. // Note that we do not carry over the left/top/front here, since the returned
  275. // PixelBox will be re-based from the locking point onwards
  276. PixelData rval(lockBox.getWidth(), lockBox.getHeight(), lockBox.getDepth(), mFormat);
  277. if (bufferResources->surface != NULL)
  278. {
  279. // Surface
  280. D3DLOCKED_RECT lrect; // Filled in by D3D
  281. HRESULT hr;
  282. if (lockBox.left == 0 && lockBox.top == 0
  283. && lockBox.right == mWidth && lockBox.bottom == mHeight)
  284. {
  285. // Lock whole surface
  286. hr = bufferResources->surface->LockRect(&lrect, NULL, flags);
  287. }
  288. else
  289. {
  290. RECT prect = toD3DRECT(lockBox); // specify range to lock
  291. hr = bufferResources->surface->LockRect(&lrect, &prect, flags);
  292. }
  293. if (FAILED(hr))
  294. CM_EXCEPT(RenderingAPIException, "Surface locking failed");
  295. fromD3DLock(rval, lrect);
  296. }
  297. else if(bufferResources->volume)
  298. {
  299. // Volume
  300. D3DBOX pbox = toD3DBOX(lockBox); // specify range to lock
  301. D3DLOCKED_BOX lbox; // Filled in by D3D
  302. if(bufferResources->volume->LockBox(&lbox, &pbox, flags) != D3D_OK)
  303. CM_EXCEPT(RenderingAPIException, "Volume locking failed");
  304. fromD3DLock(rval, lbox);
  305. }
  306. return rval;
  307. }
  308. //-----------------------------------------------------------------------------
  309. void D3D9PixelBuffer::unlockImpl(void)
  310. {
  311. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  312. if (mMapDeviceToBufferResources.size() == 0)
  313. {
  314. CM_EXCEPT(RenderingAPIException, "There are no resources attached to this pixel buffer !!");
  315. }
  316. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  317. unlockBuffer( it->second);
  318. if(mDoMipmapGen)
  319. _genMipmaps(it->second->mipTex);
  320. }
  321. //-----------------------------------------------------------------------------
  322. void D3D9PixelBuffer::unlockBuffer(BufferResources* bufferResources)
  323. {
  324. if(bufferResources->surface)
  325. {
  326. // Surface
  327. bufferResources->surface->UnlockRect();
  328. }
  329. else if(bufferResources->volume)
  330. {
  331. // Volume
  332. bufferResources->volume->UnlockBox();
  333. }
  334. }
  335. //-----------------------------------------------------------------------------
  336. void D3D9PixelBuffer::_genMipmaps(IDirect3DBaseTexture9* mipTex)
  337. {
  338. assert(mipTex);
  339. // Mipmapping
  340. if (mHWMipmaps)
  341. {
  342. // Hardware mipmaps
  343. mipTex->GenerateMipSubLevels();
  344. }
  345. else
  346. {
  347. // Software mipmaps
  348. if( D3DXFilterTexture( mipTex, NULL, D3DX_DEFAULT, D3DX_DEFAULT ) != D3D_OK )
  349. {
  350. CM_EXCEPT(RenderingAPIException, "Failed to filter texture (generate mipmaps)");
  351. }
  352. }
  353. }
  354. //-----------------------------------------------------------------------------
  355. void D3D9PixelBuffer::_setMipmapping(bool doMipmapGen,
  356. bool HWMipmaps)
  357. {
  358. mDoMipmapGen = doMipmapGen;
  359. mHWMipmaps = HWMipmaps;
  360. }
  361. //-----------------------------------------------------------------------------
  362. void D3D9PixelBuffer::releaseSurfaces(IDirect3DDevice9* d3d9Device)
  363. {
  364. BufferResources* bufferResources = getBufferResources(d3d9Device);
  365. if (bufferResources != NULL)
  366. {
  367. SAFE_RELEASE(bufferResources->surface);
  368. SAFE_RELEASE(bufferResources->volume);
  369. }
  370. }
  371. //-----------------------------------------------------------------------------
  372. IDirect3DSurface9* D3D9PixelBuffer::getSurface(IDirect3DDevice9* d3d9Device)
  373. {
  374. BufferResources* bufferResources = getBufferResources(d3d9Device);
  375. if (bufferResources == NULL)
  376. {
  377. mOwnerTexture->createTextureResources(d3d9Device);
  378. bufferResources = getBufferResources(d3d9Device);
  379. }
  380. return bufferResources->surface;
  381. }
  382. };