CmD3D9PixelBuffer.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmD3D9PixelBuffer.h"
  25. #include "CmD3D9Texture.h"
  26. #include "CmD3D9Mappings.h"
  27. #include "CmException.h"
  28. #include "CmBitwise.h"
  29. #include "CmRenderSystem.h"
  30. namespace CamelotFramework
  31. {
  32. CM_STATIC_MUTEX_INSTANCE(D3D9PixelBuffer::msDeviceAccessMutex)
  33. //-----------------------------------------------------------------------------
  34. D3D9PixelBuffer::D3D9PixelBuffer(GpuBufferUsage usage,
  35. D3D9Texture* ownerTexture):
  36. PixelBuffer(0, 0, 0, PF_UNKNOWN, usage, false),
  37. mDoMipmapGen(0), mHWMipmaps(0), mOwnerTexture(ownerTexture)
  38. {
  39. }
  40. D3D9PixelBuffer::~D3D9PixelBuffer()
  41. {
  42. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  43. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  44. while (it != mMapDeviceToBufferResources.end())
  45. {
  46. SAFE_RELEASE(it->second->surface);
  47. SAFE_RELEASE(it->second->volume);
  48. if(it->second != nullptr)
  49. cm_delete<PoolAlloc>(it->second);
  50. DeviceToBufferResourcesIterator deadi = it++;
  51. mMapDeviceToBufferResources.erase(deadi);
  52. }
  53. }
  54. //-----------------------------------------------------------------------------
  55. void D3D9PixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DSurface9 *surface,
  56. bool writeGamma, UINT32 fsaa, const String& srcName,
  57. IDirect3DBaseTexture9 *mipTex)
  58. {
  59. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  60. BufferResources* bufferResources = getBufferResources(dev);
  61. bool isNewBuffer = false;
  62. if (bufferResources == NULL)
  63. {
  64. bufferResources = createBufferResources();
  65. mMapDeviceToBufferResources[dev] = bufferResources;
  66. isNewBuffer = true;
  67. }
  68. bufferResources->mipTex = mipTex;
  69. bufferResources->surface = surface;
  70. bufferResources->surface->AddRef();
  71. D3DSURFACE_DESC desc;
  72. if(surface->GetDesc(&desc) != D3D_OK)
  73. CM_EXCEPT(RenderingAPIException, "Could not get surface information");
  74. mWidth = desc.Width;
  75. mHeight = desc.Height;
  76. mDepth = 1;
  77. mFormat = D3D9Mappings::_getPF(desc.Format);
  78. // Default
  79. mRowPitch = mWidth;
  80. mSlicePitch = mHeight*mWidth;
  81. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  82. // TODO PORT - My Texture doesn't inherit from Resource and doesn't have that method. Not sure why it needs to call it,
  83. // but since we're not there's potential for trouble here.
  84. if (isNewBuffer /*&& mOwnerTexture->isManuallyLoaded()*/)
  85. {
  86. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  87. while (it != mMapDeviceToBufferResources.end())
  88. {
  89. if (it->second != bufferResources &&
  90. it->second->surface != NULL &&
  91. it->first->TestCooperativeLevel() == D3D_OK &&
  92. dev->TestCooperativeLevel() == D3D_OK)
  93. {
  94. Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
  95. PixelData dstBox(fullBufferBox, mFormat);
  96. dstBox.allocateInternalBuffer();
  97. blitToMemory(fullBufferBox, dstBox, it->second, it->first);
  98. blitFromMemory(dstBox, fullBufferBox, bufferResources);
  99. dstBox.freeInternalBuffer();
  100. break;
  101. }
  102. ++it;
  103. }
  104. }
  105. }
  106. //-----------------------------------------------------------------------------
  107. void D3D9PixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DVolume9 *volume, IDirect3DBaseTexture9 *mipTex)
  108. {
  109. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  110. BufferResources* bufferResources = getBufferResources(dev);
  111. bool isNewBuffer = false;
  112. if (bufferResources == NULL)
  113. {
  114. bufferResources = createBufferResources();
  115. mMapDeviceToBufferResources[dev] = bufferResources;
  116. isNewBuffer = true;
  117. }
  118. bufferResources->mipTex = mipTex;
  119. bufferResources->volume = volume;
  120. bufferResources->volume->AddRef();
  121. D3DVOLUME_DESC desc;
  122. if(volume->GetDesc(&desc) != D3D_OK)
  123. CM_EXCEPT(RenderingAPIException, "Could not get volume information");
  124. mWidth = desc.Width;
  125. mHeight = desc.Height;
  126. mDepth = desc.Depth;
  127. mFormat = D3D9Mappings::_getPF(desc.Format);
  128. // Default
  129. mRowPitch = mWidth;
  130. mSlicePitch = mHeight*mWidth;
  131. mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  132. // TODO PORT - My Texture doesn't inherit from Resource and doesn't have that method. Not sure why it needs to call it,
  133. // but since we're not there's potential for trouble here.
  134. if (isNewBuffer /*&& mOwnerTexture->isManuallyLoaded()*/)
  135. {
  136. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  137. while (it != mMapDeviceToBufferResources.end())
  138. {
  139. if (it->second != bufferResources &&
  140. it->second->volume != NULL &&
  141. it->first->TestCooperativeLevel() == D3D_OK &&
  142. dev->TestCooperativeLevel() == D3D_OK)
  143. {
  144. Box fullBufferBox(0,0,0,mWidth,mHeight,mDepth);
  145. PixelData dstBox(fullBufferBox, mFormat);
  146. dstBox.allocateInternalBuffer();
  147. blitToMemory(fullBufferBox, dstBox, it->second, it->first);
  148. blitFromMemory(dstBox, fullBufferBox, bufferResources);
  149. dstBox.freeInternalBuffer();
  150. break;
  151. }
  152. ++it;
  153. }
  154. }
  155. }
  156. //-----------------------------------------------------------------------------
  157. D3D9PixelBuffer::BufferResources* D3D9PixelBuffer::getBufferResources(IDirect3DDevice9* d3d9Device)
  158. {
  159. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.find(d3d9Device);
  160. if (it != mMapDeviceToBufferResources.end())
  161. return it->second;
  162. return NULL;
  163. }
  164. //-----------------------------------------------------------------------------
  165. D3D9PixelBuffer::BufferResources* D3D9PixelBuffer::createBufferResources()
  166. {
  167. BufferResources* newResources = cm_new<BufferResources, PoolAlloc>();
  168. memset(newResources, 0, sizeof(BufferResources));
  169. return newResources;
  170. }
  171. //-----------------------------------------------------------------------------
  172. void D3D9PixelBuffer::destroyBufferResources(IDirect3DDevice9* d3d9Device)
  173. {
  174. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  175. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.find(d3d9Device);
  176. if (it != mMapDeviceToBufferResources.end())
  177. {
  178. SAFE_RELEASE(it->second->surface);
  179. SAFE_RELEASE(it->second->volume);
  180. if(it->second != nullptr)
  181. cm_delete<PoolAlloc>(it->second);
  182. mMapDeviceToBufferResources.erase(it);
  183. }
  184. }
  185. //-----------------------------------------------------------------------------
  186. void D3D9PixelBuffer::lockDeviceAccess()
  187. {
  188. D3D9_DEVICE_ACCESS_LOCK;
  189. }
  190. //-----------------------------------------------------------------------------
  191. void D3D9PixelBuffer::unlockDeviceAccess()
  192. {
  193. D3D9_DEVICE_ACCESS_UNLOCK;
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Util functions to convert a D3D locked box to a pixel box
  197. void fromD3DLock(PixelData &rval, const D3DLOCKED_RECT &lrect)
  198. {
  199. UINT32 bpp = PixelUtil::getNumElemBytes(rval.getFormat());
  200. if (bpp != 0)
  201. {
  202. rval.setRowPitch(lrect.Pitch / bpp);
  203. rval.setSlicePitch(rval.getRowPitch() * rval.getHeight());
  204. assert((lrect.Pitch % bpp)==0);
  205. }
  206. else if (PixelUtil::isCompressed(rval.getFormat()))
  207. {
  208. rval.setRowPitch(rval.getWidth());
  209. rval.setSlicePitch(rval.getWidth() * rval.getHeight());
  210. }
  211. else
  212. {
  213. CM_EXCEPT(InvalidParametersException, "Invalid pixel format");
  214. }
  215. rval.setExternalBuffer((UINT8*)lrect.pBits);
  216. }
  217. void fromD3DLock(PixelData &rval, const D3DLOCKED_BOX &lbox)
  218. {
  219. UINT32 bpp = PixelUtil::getNumElemBytes(rval.getFormat());
  220. if (bpp != 0)
  221. {
  222. rval.setRowPitch(lbox.RowPitch / bpp);
  223. rval.setSlicePitch(lbox.SlicePitch / bpp);
  224. assert((lbox.RowPitch % bpp)==0);
  225. assert((lbox.SlicePitch % bpp)==0);
  226. }
  227. else if (PixelUtil::isCompressed(rval.getFormat()))
  228. {
  229. rval.setRowPitch(rval.getWidth());
  230. rval.setSlicePitch(rval.getWidth() * rval.getHeight());
  231. }
  232. else
  233. {
  234. CM_EXCEPT(InvalidParametersException, "Invalid pixel format");
  235. }
  236. rval.setExternalBuffer((UINT8*)lbox.pBits);
  237. }
  238. // Convert Ogre integer Box to D3D rectangle
  239. RECT toD3DRECT(const Box &lockBox)
  240. {
  241. RECT prect;
  242. assert(lockBox.getDepth() == 1);
  243. prect.left = static_cast<LONG>(lockBox.left);
  244. prect.right = static_cast<LONG>(lockBox.right);
  245. prect.top = static_cast<LONG>(lockBox.top);
  246. prect.bottom = static_cast<LONG>(lockBox.bottom);
  247. return prect;
  248. }
  249. // Convert Ogre integer Box to D3D box
  250. D3DBOX toD3DBOX(const Box &lockBox)
  251. {
  252. D3DBOX pbox;
  253. pbox.Left = static_cast<UINT>(lockBox.left);
  254. pbox.Right = static_cast<UINT>(lockBox.right);
  255. pbox.Top = static_cast<UINT>(lockBox.top);
  256. pbox.Bottom = static_cast<UINT>(lockBox.bottom);
  257. pbox.Front = static_cast<UINT>(lockBox.front);
  258. pbox.Back = static_cast<UINT>(lockBox.back);
  259. return pbox;
  260. }
  261. // Convert Ogre pixelbox extent to D3D rectangle
  262. RECT toD3DRECTExtent(const PixelData &lockBox)
  263. {
  264. RECT prect;
  265. assert(lockBox.getDepth() == 1);
  266. prect.left = 0;
  267. prect.right = static_cast<LONG>(lockBox.getWidth());
  268. prect.top = 0;
  269. prect.bottom = static_cast<LONG>(lockBox.getHeight());
  270. return prect;
  271. }
  272. // Convert Ogre pixelbox extent to D3D box
  273. D3DBOX toD3DBOXExtent(const PixelData &lockBox)
  274. {
  275. D3DBOX pbox;
  276. pbox.Left = 0;
  277. pbox.Right = static_cast<UINT>(lockBox.getWidth());
  278. pbox.Top = 0;
  279. pbox.Bottom = static_cast<UINT>(lockBox.getHeight());
  280. pbox.Front = 0;
  281. pbox.Back = static_cast<UINT>(lockBox.getDepth());
  282. return pbox;
  283. }
  284. //-----------------------------------------------------------------------------
  285. PixelData D3D9PixelBuffer::lockImpl(const Box lockBox, GpuLockOptions options)
  286. {
  287. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  288. // Set locking flags according to options
  289. DWORD flags = 0;
  290. switch(options)
  291. {
  292. case GBL_WRITE_ONLY_DISCARD:
  293. // D3D only likes D3DLOCK_DISCARD if you created the texture with D3DUSAGE_DYNAMIC
  294. // debug runtime flags this up, could cause problems on some drivers
  295. if (mUsage & GBU_DYNAMIC)
  296. flags |= D3DLOCK_DISCARD;
  297. break;
  298. case GBL_READ_ONLY:
  299. flags |= D3DLOCK_READONLY;
  300. break;
  301. default:
  302. break;
  303. };
  304. if (mMapDeviceToBufferResources.size() == 0)
  305. {
  306. CM_EXCEPT(RenderingAPIException, "There are no resources attached to this pixel buffer !!");
  307. }
  308. mLockedBox = lockBox;
  309. mLockFlags = flags;
  310. BufferResources* bufferResources = mMapDeviceToBufferResources.begin()->second;
  311. // Lock the source buffer.
  312. return lockBuffer(bufferResources, lockBox, flags);
  313. }
  314. //-----------------------------------------------------------------------------
  315. CamelotFramework::PixelData D3D9PixelBuffer::lockBuffer(BufferResources* bufferResources,
  316. const Box &lockBox,
  317. DWORD flags)
  318. {
  319. // Set extents and format
  320. // Note that we do not carry over the left/top/front here, since the returned
  321. // PixelBox will be re-based from the locking point onwards
  322. PixelData rval(lockBox.getWidth(), lockBox.getHeight(), lockBox.getDepth(), mFormat);
  323. if (bufferResources->surface != NULL)
  324. {
  325. // Surface
  326. D3DLOCKED_RECT lrect; // Filled in by D3D
  327. HRESULT hr;
  328. if (lockBox.left == 0 && lockBox.top == 0
  329. && lockBox.right == mWidth && lockBox.bottom == mHeight)
  330. {
  331. // Lock whole surface
  332. hr = bufferResources->surface->LockRect(&lrect, NULL, flags);
  333. }
  334. else
  335. {
  336. RECT prect = toD3DRECT(lockBox); // specify range to lock
  337. hr = bufferResources->surface->LockRect(&lrect, &prect, flags);
  338. }
  339. if (FAILED(hr))
  340. CM_EXCEPT(RenderingAPIException, "Surface locking failed");
  341. fromD3DLock(rval, lrect);
  342. }
  343. else if(bufferResources->volume)
  344. {
  345. // Volume
  346. D3DBOX pbox = toD3DBOX(lockBox); // specify range to lock
  347. D3DLOCKED_BOX lbox; // Filled in by D3D
  348. if(bufferResources->volume->LockBox(&lbox, &pbox, flags) != D3D_OK)
  349. CM_EXCEPT(RenderingAPIException, "Volume locking failed");
  350. fromD3DLock(rval, lbox);
  351. }
  352. return rval;
  353. }
  354. //-----------------------------------------------------------------------------
  355. void D3D9PixelBuffer::unlockImpl(void)
  356. {
  357. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  358. if (mMapDeviceToBufferResources.size() == 0)
  359. {
  360. CM_EXCEPT(RenderingAPIException, "There are no resources attached to this pixel buffer !!");
  361. }
  362. DeviceToBufferResourcesIterator it;
  363. // 1. Update duplicates buffers.
  364. it = mMapDeviceToBufferResources.begin();
  365. ++it;
  366. while (it != mMapDeviceToBufferResources.end())
  367. {
  368. BufferResources* bufferResources = it->second;
  369. // Update duplicated buffer from the from the locked buffer content.
  370. blitFromMemory(mCurrentLock, mLockedBox, bufferResources);
  371. ++it;
  372. }
  373. // 2. Unlock the locked buffer.
  374. it = mMapDeviceToBufferResources.begin();
  375. unlockBuffer( it->second);
  376. if(mDoMipmapGen)
  377. _genMipmaps(it->second->mipTex);
  378. }
  379. //-----------------------------------------------------------------------------
  380. void D3D9PixelBuffer::unlockBuffer(BufferResources* bufferResources)
  381. {
  382. if(bufferResources->surface)
  383. {
  384. // Surface
  385. bufferResources->surface->UnlockRect();
  386. }
  387. else if(bufferResources->volume)
  388. {
  389. // Volume
  390. bufferResources->volume->UnlockBox();
  391. }
  392. }
  393. //-----------------------------------------------------------------------------
  394. void D3D9PixelBuffer::blit(const PixelBufferPtr &rsrc,
  395. const Box &srcBox,
  396. const Box &dstBox)
  397. {
  398. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  399. D3D9PixelBuffer *src = static_cast<D3D9PixelBuffer*>(rsrc.get());
  400. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  401. // Update all the buffer copies.
  402. while (it != mMapDeviceToBufferResources.end())
  403. {
  404. BufferResources* srcBufferResources = src->getBufferResources(it->first);
  405. BufferResources* dstBufferResources = it->second;
  406. if (srcBufferResources == NULL)
  407. {
  408. CM_EXCEPT(RenderingAPIException, "There are no matching resources attached to the source pixel buffer !!");
  409. }
  410. blit(it->first, rsrc, srcBox, dstBox, srcBufferResources, dstBufferResources);
  411. ++it;
  412. }
  413. }
  414. //-----------------------------------------------------------------------------
  415. void D3D9PixelBuffer::blit(IDirect3DDevice9* d3d9Device,
  416. const PixelBufferPtr &rsrc,
  417. const Box &srcBox,
  418. const Box &dstBox,
  419. BufferResources* srcBufferResources,
  420. BufferResources* dstBufferResources)
  421. {
  422. if(dstBufferResources->surface && srcBufferResources->surface)
  423. {
  424. // Surface-to-surface
  425. RECT dsrcRect = toD3DRECT(srcBox);
  426. RECT ddestRect = toD3DRECT(dstBox);
  427. D3DSURFACE_DESC srcDesc;
  428. if(srcBufferResources->surface->GetDesc(&srcDesc) != D3D_OK)
  429. CM_EXCEPT(RenderingAPIException, "Could not get surface information");
  430. // If we're blitting from a RTT, try GetRenderTargetData
  431. // if we're going to try to use GetRenderTargetData, need to use system mem pool
  432. bool tryGetRenderTargetData = false;
  433. if ((srcDesc.Usage & D3DUSAGE_RENDERTARGET) != 0
  434. && srcDesc.MultiSampleType == D3DMULTISAMPLE_NONE)
  435. {
  436. // Temp texture
  437. IDirect3DTexture9 *tmptex;
  438. IDirect3DSurface9 *tmpsurface;
  439. if(D3DXCreateTexture(
  440. d3d9Device,
  441. srcDesc.Width, srcDesc.Height,
  442. 1, // 1 mip level ie topmost, generate no mipmaps
  443. 0, srcDesc.Format, D3DPOOL_SYSTEMMEM,
  444. &tmptex
  445. ) != D3D_OK)
  446. {
  447. CM_EXCEPT(RenderingAPIException, "Create temporary texture failed");
  448. }
  449. if(tmptex->GetSurfaceLevel(0, &tmpsurface) != D3D_OK)
  450. {
  451. tmptex->Release();
  452. CM_EXCEPT(RenderingAPIException, "Get surface level failed");
  453. }
  454. if(d3d9Device->GetRenderTargetData(srcBufferResources->surface, tmpsurface) == D3D_OK)
  455. {
  456. // Hey, it worked
  457. // Copy from this surface instead
  458. if(D3DXLoadSurfaceFromSurface(
  459. dstBufferResources->surface, NULL, &ddestRect,
  460. tmpsurface, NULL, &dsrcRect,
  461. D3DX_DEFAULT, 0) != D3D_OK)
  462. {
  463. tmpsurface->Release();
  464. tmptex->Release();
  465. CM_EXCEPT(RenderingAPIException, "D3DXLoadSurfaceFromSurface failed");
  466. }
  467. tmpsurface->Release();
  468. tmptex->Release();
  469. return;
  470. }
  471. }
  472. // Otherwise, try the normal method
  473. // D3DXLoadSurfaceFromSurface
  474. if(D3DXLoadSurfaceFromSurface(
  475. dstBufferResources->surface, NULL, &ddestRect,
  476. srcBufferResources->surface, NULL, &dsrcRect,
  477. D3DX_DEFAULT, 0) != D3D_OK)
  478. {
  479. CM_EXCEPT(RenderingAPIException, "D3DXLoadSurfaceFromSurface failed");
  480. }
  481. }
  482. else if(dstBufferResources->volume && srcBufferResources->volume)
  483. {
  484. // Volume-to-volume
  485. D3DBOX dsrcBox = toD3DBOX(srcBox);
  486. D3DBOX ddestBox = toD3DBOX(dstBox);
  487. // D3DXLoadVolumeFromVolume
  488. if(D3DXLoadVolumeFromVolume(
  489. dstBufferResources->volume, NULL, &ddestBox,
  490. srcBufferResources->volume, NULL, &dsrcBox,
  491. D3DX_DEFAULT, 0) != D3D_OK)
  492. {
  493. CM_EXCEPT(RenderingAPIException, "D3DXLoadVolumeFromVolume failed");
  494. }
  495. }
  496. else
  497. {
  498. // Software fallback
  499. PixelBuffer::blit(rsrc, srcBox, dstBox);
  500. }
  501. }
  502. //-----------------------------------------------------------------------------
  503. void D3D9PixelBuffer::blitFromMemory(const PixelData &src, const Box &dstBox)
  504. {
  505. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  506. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  507. while (it != mMapDeviceToBufferResources.end())
  508. {
  509. BufferResources* dstBufferResources = it->second;
  510. blitFromMemory(src, dstBox, dstBufferResources);
  511. ++it;
  512. }
  513. }
  514. //-----------------------------------------------------------------------------
  515. void D3D9PixelBuffer::blitFromMemory(const PixelData &src, const Box &dstBox, BufferResources* dstBufferResources)
  516. {
  517. // for scoped deletion of conversion buffer
  518. PixelData converted = src;
  519. // convert to pixelbuffer's native format if necessary
  520. if (D3D9Mappings::_getPF(src.getFormat()) == D3DFMT_UNKNOWN)
  521. {
  522. converted = PixelData(src.getWidth(), src.getHeight(), src.getDepth(), mFormat);
  523. converted.allocateInternalBuffer();
  524. PixelUtil::bulkPixelConversion(src, converted);
  525. }
  526. UINT32 rowWidth;
  527. if (PixelUtil::isCompressed(converted.getFormat()))
  528. {
  529. // D3D wants the width of one row of cells in bytes
  530. if (converted.getFormat() == PF_DXT1)
  531. {
  532. // 64 bits (8 bytes) per 4x4 block
  533. rowWidth = (converted.getRowPitch() / 4) * 8;
  534. }
  535. else
  536. {
  537. // 128 bits (16 bytes) per 4x4 block
  538. rowWidth = (converted.getRowPitch() / 4) * 16;
  539. }
  540. }
  541. else
  542. {
  543. rowWidth = converted.getRowPitch() * PixelUtil::getNumElemBytes(converted.getFormat());
  544. }
  545. if (dstBufferResources->surface)
  546. {
  547. RECT destRect, srcRect;
  548. srcRect = toD3DRECT(converted.getExtents());
  549. destRect = toD3DRECT(dstBox);
  550. if(D3DXLoadSurfaceFromMemory(dstBufferResources->surface, NULL, &destRect,
  551. converted.getData(), D3D9Mappings::_getPF(converted.getFormat()),
  552. static_cast<UINT>(rowWidth),
  553. NULL, &srcRect, D3DX_DEFAULT, 0) != D3D_OK)
  554. {
  555. CM_EXCEPT(RenderingAPIException, "D3DXLoadSurfaceFromMemory failed");
  556. }
  557. }
  558. else if (dstBufferResources->volume)
  559. {
  560. D3DBOX destBox, srcBox;
  561. srcBox = toD3DBOX(converted.getExtents());
  562. destBox = toD3DBOX(dstBox);
  563. UINT32 sliceWidth;
  564. if (PixelUtil::isCompressed(converted.getFormat()))
  565. {
  566. // D3D wants the width of one slice of cells in bytes
  567. if (converted.getFormat() == PF_DXT1)
  568. {
  569. // 64 bits (8 bytes) per 4x4 block
  570. sliceWidth = (converted.getSlicePitch() / 16) * 8;
  571. }
  572. else
  573. {
  574. // 128 bits (16 bytes) per 4x4 block
  575. sliceWidth = (converted.getSlicePitch() / 16) * 16;
  576. }
  577. }
  578. else
  579. {
  580. sliceWidth = converted.getSlicePitch() * PixelUtil::getNumElemBytes(converted.getFormat());
  581. }
  582. if(D3DXLoadVolumeFromMemory(dstBufferResources->volume, NULL, &destBox,
  583. converted.getData(), D3D9Mappings::_getPF(converted.getFormat()),
  584. static_cast<UINT>(rowWidth), static_cast<UINT>(sliceWidth),
  585. NULL, &srcBox, D3DX_DEFAULT, 0) != D3D_OK)
  586. {
  587. CM_EXCEPT(RenderingAPIException, "D3DXLoadSurfaceFromMemory failed");
  588. }
  589. }
  590. if(mDoMipmapGen)
  591. _genMipmaps(dstBufferResources->mipTex);
  592. converted.freeInternalBuffer();
  593. }
  594. //-----------------------------------------------------------------------------
  595. void D3D9PixelBuffer::blitToMemory(const Box &srcBox, const PixelData &dst)
  596. {
  597. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  598. DeviceToBufferResourcesIterator it = mMapDeviceToBufferResources.begin();
  599. BufferResources* bufferResources = it->second;
  600. blitToMemory(srcBox, dst, bufferResources, it->first);
  601. }
  602. //-----------------------------------------------------------------------------
  603. void D3D9PixelBuffer::blitToMemory(const Box &srcBox, const PixelData &dst,
  604. BufferResources* srcBufferResources,
  605. IDirect3DDevice9* d3d9Device)
  606. {
  607. // Decide on pixel format of temp surface
  608. PixelFormat tmpFormat = mFormat;
  609. if(D3D9Mappings::_getPF(dst.getFormat()) != D3DFMT_UNKNOWN)
  610. {
  611. tmpFormat = dst.getFormat();
  612. }
  613. if (srcBufferResources->surface)
  614. {
  615. assert(srcBox.getDepth() == 1 && dst.getDepth() == 1);
  616. // Create temp texture
  617. IDirect3DTexture9 *tmp;
  618. IDirect3DSurface9 *surface;
  619. D3DSURFACE_DESC srcDesc;
  620. if(srcBufferResources->surface->GetDesc(&srcDesc) != D3D_OK)
  621. CM_EXCEPT(RenderingAPIException, "Could not get surface information");
  622. D3DPOOL temppool = D3DPOOL_SCRATCH;
  623. // if we're going to try to use GetRenderTargetData, need to use system mem pool
  624. bool tryGetRenderTargetData = false;
  625. if (((srcDesc.Usage & D3DUSAGE_RENDERTARGET) != 0) &&
  626. (srcBox.getWidth() == dst.getWidth()) && (srcBox.getHeight() == dst.getHeight()) &&
  627. (srcBox.getWidth() == getWidth()) && (srcBox.getHeight() == getHeight()) &&
  628. (mFormat == tmpFormat))
  629. {
  630. tryGetRenderTargetData = true;
  631. temppool = D3DPOOL_SYSTEMMEM;
  632. }
  633. if(D3DXCreateTexture(
  634. d3d9Device,
  635. static_cast<UINT>(dst.getWidth()), static_cast<UINT>(dst.getHeight()),
  636. 1, // 1 mip level ie topmost, generate no mipmaps
  637. 0, D3D9Mappings::_getPF(tmpFormat), temppool,
  638. &tmp
  639. ) != D3D_OK)
  640. {
  641. CM_EXCEPT(RenderingAPIException, "Create temporary texture failed");
  642. }
  643. if(tmp->GetSurfaceLevel(0, &surface) != D3D_OK)
  644. {
  645. tmp->Release();
  646. CM_EXCEPT(RenderingAPIException, "Get surface level failed");
  647. }
  648. // Copy texture to this temp surface
  649. RECT destRect, srcRect;
  650. srcRect = toD3DRECT(srcBox);
  651. destRect = toD3DRECTExtent(dst);
  652. // Get the real temp surface format
  653. D3DSURFACE_DESC dstDesc;
  654. if(surface->GetDesc(&dstDesc) != D3D_OK)
  655. CM_EXCEPT(RenderingAPIException, "Could not get surface information");
  656. tmpFormat = D3D9Mappings::_getPF(dstDesc.Format);
  657. // Use fast GetRenderTargetData if we are in its usage conditions
  658. bool fastLoadSuccess = false;
  659. if (tryGetRenderTargetData)
  660. {
  661. if(d3d9Device->GetRenderTargetData(srcBufferResources->surface, surface) == D3D_OK)
  662. {
  663. fastLoadSuccess = true;
  664. }
  665. }
  666. if (!fastLoadSuccess)
  667. {
  668. if(D3DXLoadSurfaceFromSurface(
  669. surface, NULL, &destRect,
  670. srcBufferResources->surface, NULL, &srcRect,
  671. D3DX_DEFAULT, 0) != D3D_OK)
  672. {
  673. surface->Release();
  674. tmp->Release();
  675. CM_EXCEPT(RenderingAPIException, "D3DXLoadSurfaceFromSurface failed");
  676. }
  677. }
  678. // Lock temp surface and copy it to memory
  679. D3DLOCKED_RECT lrect; // Filled in by D3D
  680. if(surface->LockRect(&lrect, NULL, D3DLOCK_READONLY) != D3D_OK)
  681. {
  682. surface->Release();
  683. tmp->Release();
  684. CM_EXCEPT(RenderingAPIException, "surface->LockRect");
  685. }
  686. // Copy it
  687. PixelData locked(dst.getWidth(), dst.getHeight(), dst.getDepth(), tmpFormat);
  688. fromD3DLock(locked, lrect);
  689. PixelUtil::bulkPixelConversion(locked, dst);
  690. surface->UnlockRect();
  691. // Release temporary surface and texture
  692. surface->Release();
  693. tmp->Release();
  694. }
  695. else if (srcBufferResources->volume)
  696. {
  697. // Create temp texture
  698. IDirect3DVolumeTexture9 *tmp;
  699. IDirect3DVolume9 *surface;
  700. if(D3DXCreateVolumeTexture(
  701. d3d9Device,
  702. static_cast<UINT>(dst.getWidth()),
  703. static_cast<UINT>(dst.getHeight()),
  704. static_cast<UINT>(dst.getDepth()), 0,
  705. 0, D3D9Mappings::_getPF(tmpFormat), D3DPOOL_SCRATCH,
  706. &tmp
  707. ) != D3D_OK)
  708. {
  709. CM_EXCEPT(RenderingAPIException, "Create temporary texture failed");
  710. }
  711. if(tmp->GetVolumeLevel(0, &surface) != D3D_OK)
  712. {
  713. tmp->Release();
  714. CM_EXCEPT(RenderingAPIException, "Get volume level failed");
  715. }
  716. // Volume
  717. D3DBOX ddestBox, dsrcBox;
  718. ddestBox = toD3DBOXExtent(dst);
  719. dsrcBox = toD3DBOX(srcBox);
  720. if(D3DXLoadVolumeFromVolume(
  721. surface, NULL, &ddestBox,
  722. srcBufferResources->volume, NULL, &dsrcBox,
  723. D3DX_DEFAULT, 0) != D3D_OK)
  724. {
  725. surface->Release();
  726. tmp->Release();
  727. CM_EXCEPT(RenderingAPIException, "D3DXLoadVolumeFromVolume failed");
  728. }
  729. // Lock temp surface and copy it to memory
  730. D3DLOCKED_BOX lbox; // Filled in by D3D
  731. if(surface->LockBox(&lbox, NULL, D3DLOCK_READONLY) != D3D_OK)
  732. {
  733. surface->Release();
  734. tmp->Release();
  735. CM_EXCEPT(RenderingAPIException, "surface->LockBox");
  736. }
  737. // Copy it
  738. PixelData locked(dst.getWidth(), dst.getHeight(), dst.getDepth(), tmpFormat);
  739. fromD3DLock(locked, lbox);
  740. PixelUtil::bulkPixelConversion(locked, dst);
  741. surface->UnlockBox();
  742. // Release temporary surface and texture
  743. surface->Release();
  744. tmp->Release();
  745. }
  746. }
  747. //-----------------------------------------------------------------------------
  748. void D3D9PixelBuffer::_genMipmaps(IDirect3DBaseTexture9* mipTex)
  749. {
  750. assert(mipTex);
  751. // Mipmapping
  752. if (mHWMipmaps)
  753. {
  754. // Hardware mipmaps
  755. mipTex->GenerateMipSubLevels();
  756. }
  757. else
  758. {
  759. // Software mipmaps
  760. if( D3DXFilterTexture( mipTex, NULL, D3DX_DEFAULT, D3DX_DEFAULT ) != D3D_OK )
  761. {
  762. CM_EXCEPT(RenderingAPIException, "Failed to filter texture (generate mipmaps)");
  763. }
  764. }
  765. }
  766. //-----------------------------------------------------------------------------
  767. void D3D9PixelBuffer::_setMipmapping(bool doMipmapGen,
  768. bool HWMipmaps)
  769. {
  770. mDoMipmapGen = doMipmapGen;
  771. mHWMipmaps = HWMipmaps;
  772. }
  773. //-----------------------------------------------------------------------------
  774. void D3D9PixelBuffer::releaseSurfaces(IDirect3DDevice9* d3d9Device)
  775. {
  776. BufferResources* bufferResources = getBufferResources(d3d9Device);
  777. if (bufferResources != NULL)
  778. {
  779. SAFE_RELEASE(bufferResources->surface);
  780. SAFE_RELEASE(bufferResources->volume);
  781. }
  782. }
  783. //-----------------------------------------------------------------------------
  784. IDirect3DSurface9* D3D9PixelBuffer::getSurface(IDirect3DDevice9* d3d9Device)
  785. {
  786. BufferResources* bufferResources = getBufferResources(d3d9Device);
  787. if (bufferResources == NULL)
  788. {
  789. mOwnerTexture->createTextureResources(d3d9Device);
  790. bufferResources = getBufferResources(d3d9Device);
  791. }
  792. return bufferResources->surface;
  793. }
  794. };