2
0

OgreD3D9HardwarePixelBuffer.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 "OgreD3D9HardwarePixelBuffer.h"
  25. #include "OgreD3D9Texture.h"
  26. #include "OgreD3D9Mappings.h"
  27. #include "OgreException.h"
  28. #include "OgreStringConverter.h"
  29. #include "OgreBitwise.h"
  30. #include "OgreRenderSystem.h"
  31. #include "CmRenderSystemManager.h"
  32. namespace Ogre {
  33. OGRE_STATIC_MUTEX_INSTANCE(D3D9HardwarePixelBuffer::msDeviceAccessMutex)
  34. //-----------------------------------------------------------------------------
  35. D3D9HardwarePixelBuffer::D3D9HardwarePixelBuffer(HardwareBuffer::Usage usage,
  36. D3D9Texture* ownerTexture):
  37. HardwarePixelBuffer(0, 0, 0, PF_UNKNOWN, usage, false, false),
  38. mDoMipmapGen(0), mHWMipmaps(0), mOwnerTexture(ownerTexture),
  39. mRenderTexture(NULL)
  40. {
  41. }
  42. D3D9HardwarePixelBuffer::~D3D9HardwarePixelBuffer()
  43. {
  44. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  45. destroyRenderTexture();
  46. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  47. while (it != mMapDeviceToBufferResources.end())
  48. {
  49. SAFE_RELEASE(it->second->surface);
  50. SAFE_RELEASE(it->second->volume);
  51. SAFE_DELETE(it->second);
  52. DeviceToBufferResourcesIterator deadi = it++;
  53. mMapDeviceToBufferResources.erase(deadi);
  54. }
  55. }
  56. //-----------------------------------------------------------------------------
  57. void D3D9HardwarePixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DSurface9 *surface,
  58. IDirect3DSurface9* fsaaSurface,
  59. bool writeGamma, uint fsaa, const String& srcName,
  60. IDirect3DBaseTexture9 *mipTex)
  61. {
  62. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  63. BufferResources* bufferResources = getBufferResources(dev);
  64. bool isNewBuffer = false;
  65. if (bufferResources == NULL)
  66. {
  67. bufferResources = createBufferResources();
  68. mMapDeviceToBufferResources[dev] = bufferResources;
  69. isNewBuffer = true;
  70. }
  71. bufferResources->mipTex = mipTex;
  72. bufferResources->surface = surface;
  73. bufferResources->surface->AddRef();
  74. bufferResources->fSAASurface = fsaaSurface;
  75. D3DSURFACE_DESC desc;
  76. if(surface->GetDesc(&desc) != D3D_OK)
  77. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get surface information",
  78. "D3D9HardwarePixelBuffer::D3D9HardwarePixelBuffer");
  79. mWidth = desc.Width;
  80. mHeight = desc.Height;
  81. mDepth = 1;
  82. mFormat = D3D9Mappings::_getPF(desc.Format);
  83. // Default
  84. mRowPitch = mWidth;
  85. mSlicePitch = mHeight*mWidth;
  86. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  87. if(mUsage & TU_RENDERTARGET)
  88. updateRenderTexture(writeGamma, fsaa, srcName);
  89. // TODO PORT - My Texture doesn't inherit from Resource and doesn't have that method. Not sure why it needs to call it,
  90. // but since we're not there's potential for trouble here.
  91. if (isNewBuffer /*&& mOwnerTexture->isManuallyLoaded()*/)
  92. {
  93. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  94. while (it != mMapDeviceToBufferResources.end())
  95. {
  96. if (it->second != bufferResources &&
  97. it->second->surface != NULL &&
  98. it->first->TestCooperativeLevel() == D3D_OK &&
  99. dev->TestCooperativeLevel() == D3D_OK)
  100. {
  101. Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
  102. PixelBox dstBox(fullBufferBox, mFormat);
  103. dstBox.data = new char[getSizeInBytes()];
  104. blitToMemory(fullBufferBox, dstBox, it->second, it->first);
  105. blitFromMemory(dstBox, fullBufferBox, bufferResources);
  106. SAFE_DELETE_ARRAY(dstBox.data);
  107. break;
  108. }
  109. ++it;
  110. }
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. void D3D9HardwarePixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DVolume9 *volume, IDirect3DBaseTexture9 *mipTex)
  115. {
  116. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  117. BufferResources* bufferResources = getBufferResources(dev);
  118. bool isNewBuffer = false;
  119. if (bufferResources == NULL)
  120. {
  121. bufferResources = createBufferResources();
  122. mMapDeviceToBufferResources[dev] = bufferResources;
  123. isNewBuffer = true;
  124. }
  125. bufferResources->mipTex = mipTex;
  126. bufferResources->volume = volume;
  127. bufferResources->volume->AddRef();
  128. D3DVOLUME_DESC desc;
  129. if(volume->GetDesc(&desc) != D3D_OK)
  130. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get volume information",
  131. "D3D9HardwarePixelBuffer::D3D9HardwarePixelBuffer");
  132. mWidth = desc.Width;
  133. mHeight = desc.Height;
  134. mDepth = desc.Depth;
  135. mFormat = D3D9Mappings::_getPF(desc.Format);
  136. // Default
  137. mRowPitch = mWidth;
  138. mSlicePitch = mHeight*mWidth;
  139. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  140. // TODO PORT - My Texture doesn't inherit from Resource and doesn't have that method. Not sure why it needs to call it,
  141. // but since we're not there's potential for trouble here.
  142. if (isNewBuffer /*&& mOwnerTexture->isManuallyLoaded()*/)
  143. {
  144. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  145. while (it != mMapDeviceToBufferResources.end())
  146. {
  147. if (it->second != bufferResources &&
  148. it->second->volume != NULL &&
  149. it->first->TestCooperativeLevel() == D3D_OK &&
  150. dev->TestCooperativeLevel() == D3D_OK)
  151. {
  152. Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
  153. PixelBox dstBox(fullBufferBox, mFormat);
  154. dstBox.data = new char[getSizeInBytes()];
  155. blitToMemory(fullBufferBox, dstBox, it->second, it->first);
  156. blitFromMemory(dstBox, fullBufferBox, bufferResources);
  157. SAFE_DELETE(dstBox.data);
  158. break;
  159. }
  160. ++it;
  161. }
  162. }
  163. }
  164. //-----------------------------------------------------------------------------
  165. D3D9HardwarePixelBuffer::BufferResources* D3D9HardwarePixelBuffer::getBufferResources(IDirect3DDevice9* d3d9Device)
  166. {
  167. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.find(d3d9Device);
  168. if (it != mMapDeviceToBufferResources.end())
  169. return it->second;
  170. return NULL;
  171. }
  172. //-----------------------------------------------------------------------------
  173. D3D9HardwarePixelBuffer::BufferResources* D3D9HardwarePixelBuffer::createBufferResources()
  174. {
  175. BufferResources* newResources = new BufferResources;
  176. memset(newResources, 0, sizeof(BufferResources));
  177. return newResources;
  178. }
  179. //-----------------------------------------------------------------------------
  180. void D3D9HardwarePixelBuffer::destroyBufferResources(IDirect3DDevice9* d3d9Device)
  181. {
  182. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  183. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.find(d3d9Device);
  184. if (it != mMapDeviceToBufferResources.end())
  185. {
  186. SAFE_RELEASE(it->second->surface);
  187. SAFE_RELEASE(it->second->volume);
  188. SAFE_DELETE(it->second);
  189. mMapDeviceToBufferResources.erase(it);
  190. }
  191. }
  192. //-----------------------------------------------------------------------------
  193. void D3D9HardwarePixelBuffer::lockDeviceAccess()
  194. {
  195. D3D9_DEVICE_ACCESS_LOCK;
  196. }
  197. //-----------------------------------------------------------------------------
  198. void D3D9HardwarePixelBuffer::unlockDeviceAccess()
  199. {
  200. D3D9_DEVICE_ACCESS_UNLOCK;
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Util functions to convert a D3D locked box to a pixel box
  204. void fromD3DLock(PixelBox &rval, const D3DLOCKED_RECT &lrect)
  205. {
  206. size_t bpp = PixelUtil::getNumElemBytes(rval.format);
  207. if (bpp != 0)
  208. {
  209. rval.rowPitch = lrect.Pitch / bpp;
  210. rval.slicePitch = rval.rowPitch * rval.getHeight();
  211. assert((lrect.Pitch % bpp)==0);
  212. }
  213. else if (PixelUtil::isCompressed(rval.format))
  214. {
  215. rval.rowPitch = rval.getWidth();
  216. rval.slicePitch = rval.getWidth() * rval.getHeight();
  217. }
  218. else
  219. {
  220. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  221. "Invalid pixel format", "fromD3DLock");
  222. }
  223. rval.data = lrect.pBits;
  224. }
  225. void fromD3DLock(PixelBox &rval, const D3DLOCKED_BOX &lbox)
  226. {
  227. size_t bpp = PixelUtil::getNumElemBytes(rval.format);
  228. if (bpp != 0)
  229. {
  230. rval.rowPitch = lbox.RowPitch / bpp;
  231. rval.slicePitch = lbox.SlicePitch / bpp;
  232. assert((lbox.RowPitch % bpp)==0);
  233. assert((lbox.SlicePitch % bpp)==0);
  234. }
  235. else if (PixelUtil::isCompressed(rval.format))
  236. {
  237. rval.rowPitch = rval.getWidth();
  238. rval.slicePitch = rval.getWidth() * rval.getHeight();
  239. }
  240. else
  241. {
  242. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  243. "Invalid pixel format", "fromD3DLock");
  244. }
  245. rval.data = lbox.pBits;
  246. }
  247. // Convert Ogre integer Box to D3D rectangle
  248. RECT toD3DRECT(const Box &lockBox)
  249. {
  250. RECT prect;
  251. assert(lockBox.getDepth() == 1);
  252. prect.left = static_cast<LONG>(lockBox.left);
  253. prect.right = static_cast<LONG>(lockBox.right);
  254. prect.top = static_cast<LONG>(lockBox.top);
  255. prect.bottom = static_cast<LONG>(lockBox.bottom);
  256. return prect;
  257. }
  258. // Convert Ogre integer Box to D3D box
  259. D3DBOX toD3DBOX(const Box &lockBox)
  260. {
  261. D3DBOX pbox;
  262. pbox.Left = static_cast<UINT>(lockBox.left);
  263. pbox.Right = static_cast<UINT>(lockBox.right);
  264. pbox.Top = static_cast<UINT>(lockBox.top);
  265. pbox.Bottom = static_cast<UINT>(lockBox.bottom);
  266. pbox.Front = static_cast<UINT>(lockBox.front);
  267. pbox.Back = static_cast<UINT>(lockBox.back);
  268. return pbox;
  269. }
  270. // Convert Ogre pixelbox extent to D3D rectangle
  271. RECT toD3DRECTExtent(const PixelBox &lockBox)
  272. {
  273. RECT prect;
  274. assert(lockBox.getDepth() == 1);
  275. prect.left = 0;
  276. prect.right = static_cast<LONG>(lockBox.getWidth());
  277. prect.top = 0;
  278. prect.bottom = static_cast<LONG>(lockBox.getHeight());
  279. return prect;
  280. }
  281. // Convert Ogre pixelbox extent to D3D box
  282. D3DBOX toD3DBOXExtent(const PixelBox &lockBox)
  283. {
  284. D3DBOX pbox;
  285. pbox.Left = 0;
  286. pbox.Right = static_cast<UINT>(lockBox.getWidth());
  287. pbox.Top = 0;
  288. pbox.Bottom = static_cast<UINT>(lockBox.getHeight());
  289. pbox.Front = 0;
  290. pbox.Back = static_cast<UINT>(lockBox.getDepth());
  291. return pbox;
  292. }
  293. //-----------------------------------------------------------------------------
  294. PixelBox D3D9HardwarePixelBuffer::lockImpl(const Box lockBox, LockOptions options)
  295. {
  296. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  297. // Check for misuse
  298. if(mUsage & TU_RENDERTARGET)
  299. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "DirectX does not allow locking of or directly writing to RenderTargets. Use blitFromMemory if you need the contents.",
  300. "D3D9HardwarePixelBuffer::lockImpl");
  301. // Set locking flags according to options
  302. DWORD flags = 0;
  303. switch(options)
  304. {
  305. case HBL_DISCARD:
  306. // D3D only likes D3DLOCK_DISCARD if you created the texture with D3DUSAGE_DYNAMIC
  307. // debug runtime flags this up, could cause problems on some drivers
  308. if (mUsage & HBU_DYNAMIC)
  309. flags |= D3DLOCK_DISCARD;
  310. break;
  311. case HBL_READ_ONLY:
  312. flags |= D3DLOCK_READONLY;
  313. break;
  314. default:
  315. break;
  316. };
  317. if (mMapDeviceToBufferResources.size() == 0)
  318. {
  319. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "There are no resources attached to this pixel buffer !!",
  320. "D3D9HardwarePixelBuffer::lockImpl");
  321. }
  322. mLockedBox = lockBox;
  323. mLockFlags = flags;
  324. BufferResources* bufferResources = mMapDeviceToBufferResources.begin()->second;
  325. // Lock the source buffer.
  326. return lockBuffer(bufferResources, lockBox, flags);
  327. }
  328. //-----------------------------------------------------------------------------
  329. Ogre::PixelBox D3D9HardwarePixelBuffer::lockBuffer(BufferResources* bufferResources,
  330. const Box &lockBox,
  331. DWORD flags)
  332. {
  333. // Set extents and format
  334. // Note that we do not carry over the left/top/front here, since the returned
  335. // PixelBox will be re-based from the locking point onwards
  336. PixelBox rval(lockBox.getWidth(), lockBox.getHeight(), lockBox.getDepth(), mFormat);
  337. if (bufferResources->surface != NULL)
  338. {
  339. // Surface
  340. D3DLOCKED_RECT lrect; // Filled in by D3D
  341. HRESULT hr;
  342. if (lockBox.left == 0 && lockBox.top == 0
  343. && lockBox.right == mWidth && lockBox.bottom == mHeight)
  344. {
  345. // Lock whole surface
  346. hr = bufferResources->surface->LockRect(&lrect, NULL, flags);
  347. }
  348. else
  349. {
  350. RECT prect = toD3DRECT(lockBox); // specify range to lock
  351. hr = bufferResources->surface->LockRect(&lrect, &prect, flags);
  352. }
  353. if (FAILED(hr))
  354. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Surface locking failed",
  355. "D3D9HardwarePixelBuffer::lockImpl");
  356. fromD3DLock(rval, lrect);
  357. }
  358. else if(bufferResources->volume)
  359. {
  360. // Volume
  361. D3DBOX pbox = toD3DBOX(lockBox); // specify range to lock
  362. D3DLOCKED_BOX lbox; // Filled in by D3D
  363. if(bufferResources->volume->LockBox(&lbox, &pbox, flags) != D3D_OK)
  364. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Volume locking failed",
  365. "D3D9HardwarePixelBuffer::lockImpl");
  366. fromD3DLock(rval, lbox);
  367. }
  368. return rval;
  369. }
  370. //-----------------------------------------------------------------------------
  371. void D3D9HardwarePixelBuffer::unlockImpl(void)
  372. {
  373. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  374. if (mMapDeviceToBufferResources.size() == 0)
  375. {
  376. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "There are no resources attached to this pixel buffer !!",
  377. "D3D9HardwarePixelBuffer::lockImpl");
  378. }
  379. DeviceToBufferResourcesIterator it;
  380. // 1. Update duplicates buffers.
  381. it = mMapDeviceToBufferResources.begin();
  382. ++it;
  383. while (it != mMapDeviceToBufferResources.end())
  384. {
  385. BufferResources* bufferResources = it->second;
  386. // Update duplicated buffer from the from the locked buffer content.
  387. blitFromMemory(mCurrentLock, mLockedBox, bufferResources);
  388. ++it;
  389. }
  390. // 2. Unlock the locked buffer.
  391. it = mMapDeviceToBufferResources.begin();
  392. unlockBuffer( it->second);
  393. if(mDoMipmapGen)
  394. _genMipmaps(it->second->mipTex);
  395. }
  396. //-----------------------------------------------------------------------------
  397. void D3D9HardwarePixelBuffer::unlockBuffer(BufferResources* bufferResources)
  398. {
  399. if(bufferResources->surface)
  400. {
  401. // Surface
  402. bufferResources->surface->UnlockRect();
  403. }
  404. else if(bufferResources->volume)
  405. {
  406. // Volume
  407. bufferResources->volume->UnlockBox();
  408. }
  409. }
  410. //-----------------------------------------------------------------------------
  411. void D3D9HardwarePixelBuffer::blit(const HardwarePixelBufferPtr &rsrc,
  412. const Box &srcBox,
  413. const Box &dstBox)
  414. {
  415. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  416. D3D9HardwarePixelBuffer *src = static_cast<D3D9HardwarePixelBuffer*>(rsrc.get());
  417. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  418. // Update all the buffer copies.
  419. while (it != mMapDeviceToBufferResources.end())
  420. {
  421. BufferResources* srcBufferResources = src->getBufferResources(it->first);
  422. BufferResources* dstBufferResources = it->second;
  423. if (srcBufferResources == NULL)
  424. {
  425. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "There are no matching resources attached to the source pixel buffer !!",
  426. "D3D9HardwarePixelBuffer::blit");
  427. }
  428. blit(it->first, rsrc, srcBox, dstBox, srcBufferResources, dstBufferResources);
  429. ++it;
  430. }
  431. }
  432. //-----------------------------------------------------------------------------
  433. void D3D9HardwarePixelBuffer::blit(IDirect3DDevice9* d3d9Device,
  434. const HardwarePixelBufferPtr &rsrc,
  435. const Box &srcBox,
  436. const Box &dstBox,
  437. BufferResources* srcBufferResources,
  438. BufferResources* dstBufferResources)
  439. {
  440. if(dstBufferResources->surface && srcBufferResources->surface)
  441. {
  442. // Surface-to-surface
  443. RECT dsrcRect = toD3DRECT(srcBox);
  444. RECT ddestRect = toD3DRECT(dstBox);
  445. D3DSURFACE_DESC srcDesc;
  446. if(srcBufferResources->surface->GetDesc(&srcDesc) != D3D_OK)
  447. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get surface information",
  448. "D3D9HardwarePixelBuffer::blit");
  449. // If we're blitting from a RTT, try GetRenderTargetData
  450. // if we're going to try to use GetRenderTargetData, need to use system mem pool
  451. bool tryGetRenderTargetData = false;
  452. if ((srcDesc.Usage & D3DUSAGE_RENDERTARGET) != 0
  453. && srcDesc.MultiSampleType == D3DMULTISAMPLE_NONE)
  454. {
  455. // Temp texture
  456. IDirect3DTexture9 *tmptex;
  457. IDirect3DSurface9 *tmpsurface;
  458. if(D3DXCreateTexture(
  459. d3d9Device,
  460. srcDesc.Width, srcDesc.Height,
  461. 1, // 1 mip level ie topmost, generate no mipmaps
  462. 0, srcDesc.Format, D3DPOOL_SYSTEMMEM,
  463. &tmptex
  464. ) != D3D_OK)
  465. {
  466. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Create temporary texture failed",
  467. "D3D9HardwarePixelBuffer::blit");
  468. }
  469. if(tmptex->GetSurfaceLevel(0, &tmpsurface) != D3D_OK)
  470. {
  471. tmptex->Release();
  472. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Get surface level failed",
  473. "D3D9HardwarePixelBuffer::blit");
  474. }
  475. if(d3d9Device->GetRenderTargetData(srcBufferResources->surface, tmpsurface) == D3D_OK)
  476. {
  477. // Hey, it worked
  478. // Copy from this surface instead
  479. if(D3DXLoadSurfaceFromSurface(
  480. dstBufferResources->surface, NULL, &ddestRect,
  481. tmpsurface, NULL, &dsrcRect,
  482. D3DX_DEFAULT, 0) != D3D_OK)
  483. {
  484. tmpsurface->Release();
  485. tmptex->Release();
  486. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromSurface failed",
  487. "D3D9HardwarePixelBuffer::blit");
  488. }
  489. tmpsurface->Release();
  490. tmptex->Release();
  491. return;
  492. }
  493. }
  494. // Otherwise, try the normal method
  495. // D3DXLoadSurfaceFromSurface
  496. if(D3DXLoadSurfaceFromSurface(
  497. dstBufferResources->surface, NULL, &ddestRect,
  498. srcBufferResources->surface, NULL, &dsrcRect,
  499. D3DX_DEFAULT, 0) != D3D_OK)
  500. {
  501. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromSurface failed",
  502. "D3D9HardwarePixelBuffer::blit");
  503. }
  504. }
  505. else if(dstBufferResources->volume && srcBufferResources->volume)
  506. {
  507. // Volume-to-volume
  508. D3DBOX dsrcBox = toD3DBOX(srcBox);
  509. D3DBOX ddestBox = toD3DBOX(dstBox);
  510. // D3DXLoadVolumeFromVolume
  511. if(D3DXLoadVolumeFromVolume(
  512. dstBufferResources->volume, NULL, &ddestBox,
  513. srcBufferResources->volume, NULL, &dsrcBox,
  514. D3DX_DEFAULT, 0) != D3D_OK)
  515. {
  516. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadVolumeFromVolume failed",
  517. "D3D9HardwarePixelBuffer::blit");
  518. }
  519. }
  520. else
  521. {
  522. // Software fallback
  523. HardwarePixelBuffer::blit(rsrc, srcBox, dstBox);
  524. }
  525. }
  526. //-----------------------------------------------------------------------------
  527. void D3D9HardwarePixelBuffer::blitFromMemory(const PixelBox &src, const Box &dstBox)
  528. {
  529. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  530. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  531. while (it != mMapDeviceToBufferResources.end())
  532. {
  533. BufferResources* dstBufferResources = it->second;
  534. blitFromMemory(src, dstBox, dstBufferResources);
  535. ++it;
  536. }
  537. }
  538. //-----------------------------------------------------------------------------
  539. void D3D9HardwarePixelBuffer::blitFromMemory(const PixelBox &src, const Box &dstBox, BufferResources* dstBufferResources)
  540. {
  541. // for scoped deletion of conversion buffer
  542. void* data = NULL;
  543. PixelBox converted = src;
  544. // convert to pixelbuffer's native format if necessary
  545. if (D3D9Mappings::_getPF(src.format) == D3DFMT_UNKNOWN)
  546. {
  547. data = new void*[PixelUtil::getMemorySize(src.getWidth(), src.getHeight(), src.getDepth(), mFormat)];
  548. converted = PixelBox(src.getWidth(), src.getHeight(), src.getDepth(), mFormat, data);
  549. PixelUtil::bulkPixelConversion(src, converted);
  550. }
  551. size_t rowWidth;
  552. if (PixelUtil::isCompressed(converted.format))
  553. {
  554. // D3D wants the width of one row of cells in bytes
  555. if (converted.format == PF_DXT1)
  556. {
  557. // 64 bits (8 bytes) per 4x4 block
  558. rowWidth = (converted.rowPitch / 4) * 8;
  559. }
  560. else
  561. {
  562. // 128 bits (16 bytes) per 4x4 block
  563. rowWidth = (converted.rowPitch / 4) * 16;
  564. }
  565. }
  566. else
  567. {
  568. rowWidth = converted.rowPitch * PixelUtil::getNumElemBytes(converted.format);
  569. }
  570. if (dstBufferResources->surface)
  571. {
  572. RECT destRect, srcRect;
  573. srcRect = toD3DRECT(converted);
  574. destRect = toD3DRECT(dstBox);
  575. if(D3DXLoadSurfaceFromMemory(dstBufferResources->surface, NULL, &destRect,
  576. converted.data, D3D9Mappings::_getPF(converted.format),
  577. static_cast<UINT>(rowWidth),
  578. NULL, &srcRect, D3DX_DEFAULT, 0) != D3D_OK)
  579. {
  580. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromMemory failed",
  581. "D3D9HardwarePixelBuffer::blitFromMemory");
  582. }
  583. }
  584. else if (dstBufferResources->volume)
  585. {
  586. D3DBOX destBox, srcBox;
  587. srcBox = toD3DBOX(converted);
  588. destBox = toD3DBOX(dstBox);
  589. size_t sliceWidth;
  590. if (PixelUtil::isCompressed(converted.format))
  591. {
  592. // D3D wants the width of one slice of cells in bytes
  593. if (converted.format == PF_DXT1)
  594. {
  595. // 64 bits (8 bytes) per 4x4 block
  596. sliceWidth = (converted.slicePitch / 16) * 8;
  597. }
  598. else
  599. {
  600. // 128 bits (16 bytes) per 4x4 block
  601. sliceWidth = (converted.slicePitch / 16) * 16;
  602. }
  603. }
  604. else
  605. {
  606. sliceWidth = converted.slicePitch * PixelUtil::getNumElemBytes(converted.format);
  607. }
  608. if(D3DXLoadVolumeFromMemory(dstBufferResources->volume, NULL, &destBox,
  609. converted.data, D3D9Mappings::_getPF(converted.format),
  610. static_cast<UINT>(rowWidth), static_cast<UINT>(sliceWidth),
  611. NULL, &srcBox, D3DX_DEFAULT, 0) != D3D_OK)
  612. {
  613. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromMemory failed",
  614. "D3D9HardwarePixelBuffer::blitFromMemory");
  615. }
  616. }
  617. if(mDoMipmapGen)
  618. _genMipmaps(dstBufferResources->mipTex);
  619. if(data != NULL)
  620. delete[] data;
  621. }
  622. //-----------------------------------------------------------------------------
  623. void D3D9HardwarePixelBuffer::blitToMemory(const Box &srcBox, const PixelBox &dst)
  624. {
  625. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  626. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  627. BufferResources* bufferResources = it->second;
  628. blitToMemory(srcBox, dst, bufferResources, it->first);
  629. }
  630. //-----------------------------------------------------------------------------
  631. void D3D9HardwarePixelBuffer::blitToMemory(const Box &srcBox, const PixelBox &dst,
  632. BufferResources* srcBufferResources,
  633. IDirect3DDevice9* d3d9Device)
  634. {
  635. // Decide on pixel format of temp surface
  636. PixelFormat tmpFormat = mFormat;
  637. if(D3D9Mappings::_getPF(dst.format) != D3DFMT_UNKNOWN)
  638. {
  639. tmpFormat = dst.format;
  640. }
  641. if (srcBufferResources->surface)
  642. {
  643. assert(srcBox.getDepth() == 1 && dst.getDepth() == 1);
  644. // Create temp texture
  645. IDirect3DTexture9 *tmp;
  646. IDirect3DSurface9 *surface;
  647. D3DSURFACE_DESC srcDesc;
  648. if(srcBufferResources->surface->GetDesc(&srcDesc) != D3D_OK)
  649. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get surface information",
  650. "D3D9HardwarePixelBuffer::blitToMemory");
  651. D3DPOOL temppool = D3DPOOL_SCRATCH;
  652. // if we're going to try to use GetRenderTargetData, need to use system mem pool
  653. bool tryGetRenderTargetData = false;
  654. if (((srcDesc.Usage & D3DUSAGE_RENDERTARGET) != 0) &&
  655. (srcBox.getWidth() == dst.getWidth()) && (srcBox.getHeight() == dst.getHeight()) &&
  656. (srcBox.getWidth() == getWidth()) && (srcBox.getHeight() == getHeight()) &&
  657. (mFormat == tmpFormat))
  658. {
  659. tryGetRenderTargetData = true;
  660. temppool = D3DPOOL_SYSTEMMEM;
  661. }
  662. if(D3DXCreateTexture(
  663. d3d9Device,
  664. static_cast<UINT>(dst.getWidth()), static_cast<UINT>(dst.getHeight()),
  665. 1, // 1 mip level ie topmost, generate no mipmaps
  666. 0, D3D9Mappings::_getPF(tmpFormat), temppool,
  667. &tmp
  668. ) != D3D_OK)
  669. {
  670. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Create temporary texture failed",
  671. "D3D9HardwarePixelBuffer::blitToMemory");
  672. }
  673. if(tmp->GetSurfaceLevel(0, &surface) != D3D_OK)
  674. {
  675. tmp->Release();
  676. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Get surface level failed",
  677. "D3D9HardwarePixelBuffer::blitToMemory");
  678. }
  679. // Copy texture to this temp surface
  680. RECT destRect, srcRect;
  681. srcRect = toD3DRECT(srcBox);
  682. destRect = toD3DRECTExtent(dst);
  683. // Get the real temp surface format
  684. D3DSURFACE_DESC dstDesc;
  685. if(surface->GetDesc(&dstDesc) != D3D_OK)
  686. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get surface information",
  687. "D3D9HardwarePixelBuffer::blitToMemory");
  688. tmpFormat = D3D9Mappings::_getPF(dstDesc.Format);
  689. // Use fast GetRenderTargetData if we are in its usage conditions
  690. bool fastLoadSuccess = false;
  691. if (tryGetRenderTargetData)
  692. {
  693. if(d3d9Device->GetRenderTargetData(srcBufferResources->surface, surface) == D3D_OK)
  694. {
  695. fastLoadSuccess = true;
  696. }
  697. }
  698. if (!fastLoadSuccess)
  699. {
  700. if(D3DXLoadSurfaceFromSurface(
  701. surface, NULL, &destRect,
  702. srcBufferResources->surface, NULL, &srcRect,
  703. D3DX_DEFAULT, 0) != D3D_OK)
  704. {
  705. surface->Release();
  706. tmp->Release();
  707. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromSurface failed",
  708. "D3D9HardwarePixelBuffer::blitToMemory");
  709. }
  710. }
  711. // Lock temp surface and copy it to memory
  712. D3DLOCKED_RECT lrect; // Filled in by D3D
  713. if(surface->LockRect(&lrect, NULL, D3DLOCK_READONLY) != D3D_OK)
  714. {
  715. surface->Release();
  716. tmp->Release();
  717. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "surface->LockRect",
  718. "D3D9HardwarePixelBuffer::blitToMemory");
  719. }
  720. // Copy it
  721. PixelBox locked(dst.getWidth(), dst.getHeight(), dst.getDepth(), tmpFormat);
  722. fromD3DLock(locked, lrect);
  723. PixelUtil::bulkPixelConversion(locked, dst);
  724. surface->UnlockRect();
  725. // Release temporary surface and texture
  726. surface->Release();
  727. tmp->Release();
  728. }
  729. else if (srcBufferResources->volume)
  730. {
  731. // Create temp texture
  732. IDirect3DVolumeTexture9 *tmp;
  733. IDirect3DVolume9 *surface;
  734. if(D3DXCreateVolumeTexture(
  735. d3d9Device,
  736. static_cast<UINT>(dst.getWidth()),
  737. static_cast<UINT>(dst.getHeight()),
  738. static_cast<UINT>(dst.getDepth()), 0,
  739. 0, D3D9Mappings::_getPF(tmpFormat), D3DPOOL_SCRATCH,
  740. &tmp
  741. ) != D3D_OK)
  742. {
  743. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Create temporary texture failed",
  744. "D3D9HardwarePixelBuffer::blitToMemory");
  745. }
  746. if(tmp->GetVolumeLevel(0, &surface) != D3D_OK)
  747. {
  748. tmp->Release();
  749. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Get volume level failed",
  750. "D3D9HardwarePixelBuffer::blitToMemory");
  751. }
  752. // Volume
  753. D3DBOX ddestBox, dsrcBox;
  754. ddestBox = toD3DBOXExtent(dst);
  755. dsrcBox = toD3DBOX(srcBox);
  756. if(D3DXLoadVolumeFromVolume(
  757. surface, NULL, &ddestBox,
  758. srcBufferResources->volume, NULL, &dsrcBox,
  759. D3DX_DEFAULT, 0) != D3D_OK)
  760. {
  761. surface->Release();
  762. tmp->Release();
  763. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadVolumeFromVolume failed",
  764. "D3D9HardwarePixelBuffer::blitToMemory");
  765. }
  766. // Lock temp surface and copy it to memory
  767. D3DLOCKED_BOX lbox; // Filled in by D3D
  768. if(surface->LockBox(&lbox, NULL, D3DLOCK_READONLY) != D3D_OK)
  769. {
  770. surface->Release();
  771. tmp->Release();
  772. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "surface->LockBox",
  773. "D3D9HardwarePixelBuffer::blitToMemory");
  774. }
  775. // Copy it
  776. PixelBox locked(dst.getWidth(), dst.getHeight(), dst.getDepth(), tmpFormat);
  777. fromD3DLock(locked, lbox);
  778. PixelUtil::bulkPixelConversion(locked, dst);
  779. surface->UnlockBox();
  780. // Release temporary surface and texture
  781. surface->Release();
  782. tmp->Release();
  783. }
  784. }
  785. //-----------------------------------------------------------------------------
  786. void D3D9HardwarePixelBuffer::_genMipmaps(IDirect3DBaseTexture9* mipTex)
  787. {
  788. assert(mipTex);
  789. // Mipmapping
  790. if (mHWMipmaps)
  791. {
  792. // Hardware mipmaps
  793. mipTex->GenerateMipSubLevels();
  794. }
  795. else
  796. {
  797. // Software mipmaps
  798. if( D3DXFilterTexture( mipTex, NULL, D3DX_DEFAULT, D3DX_DEFAULT ) != D3D_OK )
  799. {
  800. OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR,
  801. "Failed to filter texture (generate mipmaps)",
  802. "D3D9HardwarePixelBuffer::_genMipmaps" );
  803. }
  804. }
  805. }
  806. //-----------------------------------------------------------------------------
  807. void D3D9HardwarePixelBuffer::_setMipmapping(bool doMipmapGen,
  808. bool HWMipmaps)
  809. {
  810. mDoMipmapGen = doMipmapGen;
  811. mHWMipmaps = HWMipmaps;
  812. }
  813. //-----------------------------------------------------------------------------
  814. void D3D9HardwarePixelBuffer::_clearSliceRTT(size_t zoffset)
  815. {
  816. mRenderTexture = NULL;
  817. }
  818. //-----------------------------------------------------------------------------
  819. void D3D9HardwarePixelBuffer::releaseSurfaces(IDirect3DDevice9* d3d9Device)
  820. {
  821. BufferResources* bufferResources = getBufferResources(d3d9Device);
  822. if (bufferResources != NULL)
  823. {
  824. SAFE_RELEASE(bufferResources->surface);
  825. SAFE_RELEASE(bufferResources->volume);
  826. }
  827. }
  828. //-----------------------------------------------------------------------------
  829. IDirect3DSurface9* D3D9HardwarePixelBuffer::getSurface(IDirect3DDevice9* d3d9Device)
  830. {
  831. BufferResources* bufferResources = getBufferResources(d3d9Device);
  832. if (bufferResources == NULL)
  833. {
  834. mOwnerTexture->createTextureResources(d3d9Device);
  835. bufferResources = getBufferResources(d3d9Device);
  836. }
  837. return bufferResources->surface;
  838. }
  839. //-----------------------------------------------------------------------------
  840. IDirect3DSurface9* D3D9HardwarePixelBuffer::getFSAASurface(IDirect3DDevice9* d3d9Device)
  841. {
  842. BufferResources* bufferResources = getBufferResources(d3d9Device);
  843. if (bufferResources == NULL)
  844. {
  845. mOwnerTexture->createTextureResources(d3d9Device);
  846. bufferResources = getBufferResources(d3d9Device);
  847. }
  848. return bufferResources->fSAASurface;
  849. }
  850. //-----------------------------------------------------------------------------
  851. RenderTexture *D3D9HardwarePixelBuffer::getRenderTarget(size_t zoffset)
  852. {
  853. assert(mUsage & TU_RENDERTARGET);
  854. assert(mRenderTexture != NULL);
  855. return mRenderTexture;
  856. }
  857. //-----------------------------------------------------------------------------
  858. void D3D9HardwarePixelBuffer::updateRenderTexture(bool writeGamma, uint fsaa, const String& srcName)
  859. {
  860. if (mRenderTexture == NULL)
  861. {
  862. String name;
  863. name = "rtt/" +Ogre::StringConverter::toString((size_t)this) + "/" + srcName;
  864. mRenderTexture = new D3D9RenderTexture(name, this, writeGamma, fsaa);
  865. CamelotEngine::RenderSystemManager::getActive()->attachRenderTarget(*mRenderTexture);
  866. }
  867. }
  868. //-----------------------------------------------------------------------------
  869. void D3D9HardwarePixelBuffer::destroyRenderTexture()
  870. {
  871. if (mRenderTexture != NULL)
  872. {
  873. CamelotEngine::RenderSystemManager::getActive()->destroyRenderTarget(mRenderTexture->getName());
  874. mRenderTexture = NULL;
  875. }
  876. }
  877. };