CmD3D9HardwarePixelBuffer.cpp 31 KB

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