CmD3D9PixelBuffer.cpp 13 KB

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