CmD3D9HardwarePixelBuffer.cpp 30 KB

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