OgreD3D9Device.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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 "OgreD3D9Device.h"
  25. #include "OgreD3D9DeviceManager.h"
  26. #include "OgreD3D9Driver.h"
  27. #include "OgreD3D9RenderSystem.h"
  28. #include "OgreD3D9ResourceManager.h"
  29. #include "OgreD3D9RenderWindow.h"
  30. #include "OgreHardwareBufferManager.h"
  31. #include "OgreException.h"
  32. #include "CmRenderSystemManager.h"
  33. namespace Ogre
  34. {
  35. HWND D3D9Device::msSharedFocusWindow = NULL;
  36. //---------------------------------------------------------------------
  37. D3D9Device::D3D9Device(D3D9DeviceManager* deviceManager,
  38. UINT adapterNumber,
  39. HMONITOR hMonitor,
  40. D3DDEVTYPE devType,
  41. DWORD behaviorFlags)
  42. {
  43. mpDeviceManager = deviceManager;
  44. mpDevice = NULL;
  45. mAdapterNumber = adapterNumber;
  46. mMonitor = hMonitor;
  47. mDeviceType = devType;
  48. mFocusWindow = NULL;
  49. mBehaviorFlags = behaviorFlags;
  50. mD3D9DeviceCapsValid = false;
  51. mDeviceLost = false;
  52. mPresentationParamsCount = 0;
  53. mPresentationParams = NULL;
  54. memset(&mD3D9DeviceCaps, 0, sizeof(mD3D9DeviceCaps));
  55. memset(&mCreationParams, 0, sizeof(mCreationParams));
  56. }
  57. //---------------------------------------------------------------------
  58. D3D9Device::~D3D9Device()
  59. {
  60. }
  61. //---------------------------------------------------------------------
  62. D3D9Device::RenderWindowToResorucesIterator D3D9Device::getRenderWindowIterator(D3D9RenderWindow* renderWindow)
  63. {
  64. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.find(renderWindow);
  65. if (it == mMapRenderWindowToResoruces.end())
  66. {
  67. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  68. "Render window was not attached to this device !!",
  69. "D3D9Device::getRenderWindowIterator" );
  70. }
  71. return it;
  72. }
  73. //---------------------------------------------------------------------
  74. void D3D9Device::attachRenderWindow(D3D9RenderWindow* renderWindow)
  75. {
  76. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.find(renderWindow);
  77. if (it == mMapRenderWindowToResoruces.end())
  78. {
  79. RenderWindowResources* renderWindowResources = new RenderWindowResources;
  80. memset(renderWindowResources, 0, sizeof(RenderWindowResources));
  81. renderWindowResources->adapterOrdinalInGroupIndex = 0;
  82. renderWindowResources->acquired = false;
  83. mMapRenderWindowToResoruces[renderWindow] = renderWindowResources;
  84. }
  85. updateRenderWindowsIndices();
  86. }
  87. //---------------------------------------------------------------------
  88. void D3D9Device::detachRenderWindow(D3D9RenderWindow* renderWindow)
  89. {
  90. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.find(renderWindow);
  91. if (it != mMapRenderWindowToResoruces.end())
  92. {
  93. // The focus window in which the d3d9 device created on is detached.
  94. // resources must be acquired again.
  95. if (mFocusWindow == renderWindow->getWindowHandle())
  96. {
  97. mFocusWindow = NULL;
  98. }
  99. // Case this is the shared focus window.
  100. if (renderWindow->getWindowHandle() == msSharedFocusWindow)
  101. setSharedWindowHandle(NULL);
  102. RenderWindowResources* renderWindowResources = it->second;
  103. releaseRenderWindowResources(renderWindowResources);
  104. SAFE_DELETE(renderWindowResources);
  105. mMapRenderWindowToResoruces.erase(it);
  106. }
  107. updateRenderWindowsIndices();
  108. }
  109. //---------------------------------------------------------------------
  110. bool D3D9Device::acquire()
  111. {
  112. updatePresentationParameters();
  113. bool resetDevice = false;
  114. // Create device if need to.
  115. if (mpDevice == NULL)
  116. {
  117. createD3D9Device();
  118. }
  119. // Case device already exists.
  120. else
  121. {
  122. RenderWindowToResorucesIterator itPrimary = getRenderWindowIterator(getPrimaryWindow());
  123. if (itPrimary->second->swapChain != NULL)
  124. {
  125. D3DPRESENT_PARAMETERS currentPresentParams;
  126. HRESULT hr;
  127. hr = itPrimary->second->swapChain->GetPresentParameters(&currentPresentParams);
  128. if (FAILED(hr))
  129. {
  130. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  131. "GetPresentParameters failed",
  132. "D3D9RenderWindow::acquire");
  133. }
  134. // Desired parameters are different then current parameters.
  135. // Possible scenario is that primary window resized and in the mean while another
  136. // window attached to this device.
  137. if (memcmp(&currentPresentParams, &mPresentationParams[0], sizeof(D3DPRESENT_PARAMETERS)) != 0)
  138. {
  139. resetDevice = true;
  140. }
  141. }
  142. // Make sure depth stencil is set to valid surface. It is going to be
  143. // grabbed by the primary window using the GetDepthStencilSurface method.
  144. if (resetDevice == false)
  145. {
  146. mpDevice->SetDepthStencilSurface(itPrimary->second->depthBuffer);
  147. }
  148. }
  149. // Reset device will update all render window resources.
  150. if (resetDevice)
  151. {
  152. reset();
  153. }
  154. // No need to reset -> just acquire resources.
  155. else
  156. {
  157. // Update resources of each window.
  158. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
  159. while (it != mMapRenderWindowToResoruces.end())
  160. {
  161. acquireRenderWindowResources(it);
  162. ++it;
  163. }
  164. }
  165. return true;
  166. }
  167. //---------------------------------------------------------------------
  168. void D3D9Device::release()
  169. {
  170. if (mpDevice != NULL)
  171. {
  172. D3D9RenderSystem* renderSystem = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystemManager::getActive());
  173. //// Clean up depth stencil surfaces
  174. //renderSystem->_cleanupDepthStencils(mpDevice);
  175. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
  176. while (it != mMapRenderWindowToResoruces.end())
  177. {
  178. RenderWindowResources* renderWindowResources = it->second;
  179. releaseRenderWindowResources(renderWindowResources);
  180. ++it;
  181. }
  182. releaseD3D9Device();
  183. }
  184. }
  185. //---------------------------------------------------------------------
  186. bool D3D9Device::acquire(D3D9RenderWindow* renderWindow)
  187. {
  188. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  189. acquireRenderWindowResources(it);
  190. return true;
  191. }
  192. //---------------------------------------------------------------------
  193. void D3D9Device::notifyDeviceLost()
  194. {
  195. // Case this device is already in lost state.
  196. if (mDeviceLost)
  197. return;
  198. // Case we just moved from valid state to lost state.
  199. mDeviceLost = true;
  200. D3D9RenderSystem* renderSystem = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystemManager::getActive());
  201. renderSystem->notifyOnDeviceLost(this);
  202. }
  203. //---------------------------------------------------------------------
  204. IDirect3DSurface9* D3D9Device::getDepthBuffer(D3D9RenderWindow* renderWindow)
  205. {
  206. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  207. return it->second->depthBuffer;
  208. }
  209. //---------------------------------------------------------------------
  210. IDirect3DSurface9* D3D9Device::getBackBuffer(D3D9RenderWindow* renderWindow)
  211. {
  212. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  213. return it->second->backBuffer;
  214. }
  215. //---------------------------------------------------------------------
  216. uint D3D9Device::getRenderWindowCount() const
  217. {
  218. return static_cast<uint>(mMapRenderWindowToResoruces.size());
  219. }
  220. //---------------------------------------------------------------------
  221. D3D9RenderWindow* D3D9Device::getRenderWindow(uint index)
  222. {
  223. if (index >= mMapRenderWindowToResoruces.size())
  224. {
  225. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  226. "Index of render window is out of bounds!",
  227. "D3D9RenderWindow::getRenderWindow");
  228. }
  229. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
  230. while (it != mMapRenderWindowToResoruces.end())
  231. {
  232. if (index == 0)
  233. break;
  234. --index;
  235. ++it;
  236. }
  237. return it->first;
  238. }
  239. //---------------------------------------------------------------------
  240. void D3D9Device::setAdapterOrdinalIndex(D3D9RenderWindow* renderWindow, uint adapterOrdinalInGroupIndex)
  241. {
  242. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  243. it->second->adapterOrdinalInGroupIndex = adapterOrdinalInGroupIndex;
  244. updateRenderWindowsIndices();
  245. }
  246. //---------------------------------------------------------------------
  247. void D3D9Device::destroy()
  248. {
  249. // Lock access to rendering device.
  250. D3D9RenderSystem::getResourceManager()->lockDeviceAccess();
  251. release();
  252. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
  253. if (it != mMapRenderWindowToResoruces.end())
  254. {
  255. if (it->first->getWindowHandle() == msSharedFocusWindow)
  256. setSharedWindowHandle(NULL);
  257. SAFE_DELETE(it->second);
  258. ++it;
  259. }
  260. mMapRenderWindowToResoruces.clear();
  261. // Reset dynamic attributes.
  262. mFocusWindow = NULL;
  263. mD3D9DeviceCapsValid = false;
  264. SAFE_DELETE_ARRAY(mPresentationParams);
  265. mPresentationParamsCount = 0;
  266. // Notify the device manager on this instance destruction.
  267. mpDeviceManager->notifyOnDeviceDestroy(this);
  268. // UnLock access to rendering device.
  269. D3D9RenderSystem::getResourceManager()->unlockDeviceAccess();
  270. }
  271. //---------------------------------------------------------------------
  272. bool D3D9Device::isDeviceLost()
  273. {
  274. HRESULT hr;
  275. hr = mpDevice->TestCooperativeLevel();
  276. if (hr == D3DERR_DEVICELOST ||
  277. hr == D3DERR_DEVICENOTRESET)
  278. {
  279. return true;
  280. }
  281. return false;
  282. }
  283. //---------------------------------------------------------------------
  284. bool D3D9Device::reset()
  285. {
  286. HRESULT hr;
  287. // Check that device is in valid state for reset.
  288. hr = mpDevice->TestCooperativeLevel();
  289. if (hr == D3DERR_DEVICELOST ||
  290. hr == D3DERR_DRIVERINTERNALERROR)
  291. {
  292. return false;
  293. }
  294. // Lock access to rendering device.
  295. D3D9RenderSystem::getResourceManager()->lockDeviceAccess();
  296. D3D9RenderSystem* renderSystem = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystemManager::getActive());
  297. // Inform all resources that device lost.
  298. D3D9RenderSystem::getResourceManager()->notifyOnDeviceLost(mpDevice);
  299. // Notify all listener before device is rested
  300. renderSystem->notifyOnDeviceLost(this);
  301. // Release all automatic temporary buffers and free unused
  302. // temporary buffers, so we doesn't need to recreate them,
  303. // and they will reallocate on demand. This save a lot of
  304. // release/recreate of non-managed vertex buffers which
  305. // wasn't need at all.
  306. HardwareBufferManager::getSingleton()._releaseBufferCopies(true);
  307. // Cleanup depth stencils surfaces.
  308. renderSystem->_cleanupDepthStencils(mpDevice);
  309. updatePresentationParameters();
  310. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
  311. while (it != mMapRenderWindowToResoruces.end())
  312. {
  313. RenderWindowResources* renderWindowResources = it->second;
  314. releaseRenderWindowResources(renderWindowResources);
  315. ++it;
  316. }
  317. clearDeviceStreams();
  318. // Reset the device using the presentation parameters.
  319. hr = mpDevice->Reset(mPresentationParams);
  320. if (hr == D3DERR_DEVICELOST)
  321. {
  322. // UnLock access to rendering device.
  323. D3D9RenderSystem::getResourceManager()->unlockDeviceAccess();
  324. // Don't continue
  325. return false;
  326. }
  327. else if (FAILED(hr))
  328. {
  329. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  330. "Cannot reset device!",
  331. "D3D9RenderWindow::reset");
  332. }
  333. mDeviceLost = false;
  334. // Initialize device states.
  335. setupDeviceStates();
  336. // Update resources of each window.
  337. it = mMapRenderWindowToResoruces.begin();
  338. while (it != mMapRenderWindowToResoruces.end())
  339. {
  340. acquireRenderWindowResources(it);
  341. ++it;
  342. }
  343. D3D9Device* pCurActiveDevice = mpDeviceManager->getActiveDevice();
  344. mpDeviceManager->setActiveDevice(this);
  345. // Inform all resources that device has been reset.
  346. D3D9RenderSystem::getResourceManager()->notifyOnDeviceReset(mpDevice);
  347. mpDeviceManager->setActiveDevice(pCurActiveDevice);
  348. renderSystem->notifyOnDeviceReset(this);
  349. // UnLock access to rendering device.
  350. D3D9RenderSystem::getResourceManager()->unlockDeviceAccess();
  351. return true;
  352. }
  353. //---------------------------------------------------------------------
  354. bool D3D9Device::isAutoDepthStencil() const
  355. {
  356. const D3DPRESENT_PARAMETERS& primaryPresentationParams = mPresentationParams[0];
  357. // Check if auto depth stencil can be used.
  358. for (unsigned int i = 1; i < mPresentationParamsCount; i++)
  359. {
  360. // disable AutoDepthStencil if these parameters are not all the same.
  361. if(primaryPresentationParams.BackBufferHeight != mPresentationParams[i].BackBufferHeight ||
  362. primaryPresentationParams.BackBufferWidth != mPresentationParams[i].BackBufferWidth ||
  363. primaryPresentationParams.BackBufferFormat != mPresentationParams[i].BackBufferFormat ||
  364. primaryPresentationParams.AutoDepthStencilFormat != mPresentationParams[i].AutoDepthStencilFormat ||
  365. primaryPresentationParams.MultiSampleQuality != mPresentationParams[i].MultiSampleQuality ||
  366. primaryPresentationParams.MultiSampleType != mPresentationParams[i].MultiSampleType)
  367. {
  368. return false;
  369. }
  370. }
  371. return true;
  372. }
  373. //---------------------------------------------------------------------
  374. const D3DCAPS9& D3D9Device::getD3D9DeviceCaps() const
  375. {
  376. if (mD3D9DeviceCapsValid == false)
  377. {
  378. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  379. "Device caps are invalid!",
  380. "D3D9Device::getD3D9DeviceCaps" );
  381. }
  382. return mD3D9DeviceCaps;
  383. }
  384. //---------------------------------------------------------------------
  385. D3DFORMAT D3D9Device::getBackBufferFormat() const
  386. {
  387. if (mPresentationParams == NULL || mPresentationParamsCount == 0)
  388. {
  389. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  390. "Presentation parameters are invalid!",
  391. "D3D9Device::getBackBufferFormat" );
  392. }
  393. return mPresentationParams[0].BackBufferFormat;
  394. }
  395. //---------------------------------------------------------------------
  396. IDirect3DDevice9* D3D9Device::getD3D9Device()
  397. {
  398. return mpDevice;
  399. }
  400. //---------------------------------------------------------------------
  401. void D3D9Device::updatePresentationParameters()
  402. {
  403. // Clear old presentation parameters.
  404. SAFE_DELETE_ARRAY(mPresentationParams);
  405. mPresentationParamsCount = 0;
  406. if (mMapRenderWindowToResoruces.size() > 0)
  407. {
  408. mPresentationParams = new D3DPRESENT_PARAMETERS[mMapRenderWindowToResoruces.size()];
  409. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
  410. while (it != mMapRenderWindowToResoruces.end())
  411. {
  412. D3D9RenderWindow* renderWindow = it->first;
  413. RenderWindowResources* renderWindowResources = it->second;
  414. // Ask the render window to build it's own parameters.
  415. renderWindow->buildPresentParameters(&renderWindowResources->presentParameters);
  416. // Update shared focus window handle.
  417. if (renderWindow->isFullScreen() &&
  418. renderWindowResources->presentParametersIndex == 0 &&
  419. msSharedFocusWindow == NULL)
  420. setSharedWindowHandle(renderWindow->getWindowHandle());
  421. // This is the primary window or a full screen window that is part of multi head device.
  422. if (renderWindowResources->presentParametersIndex == 0 ||
  423. renderWindow->isFullScreen())
  424. {
  425. mPresentationParams[renderWindowResources->presentParametersIndex] = renderWindowResources->presentParameters;
  426. mPresentationParamsCount++;
  427. }
  428. ++it;
  429. }
  430. }
  431. // Case we have to cancel auto depth stencil.
  432. if (isMultihead() && isAutoDepthStencil() == false)
  433. {
  434. for(unsigned int i = 0; i < mPresentationParamsCount; i++)
  435. {
  436. mPresentationParams[i].EnableAutoDepthStencil = false;
  437. }
  438. }
  439. }
  440. //---------------------------------------------------------------------
  441. UINT D3D9Device::getAdapterNumber() const
  442. {
  443. return mAdapterNumber;
  444. }
  445. //---------------------------------------------------------------------
  446. D3DDEVTYPE D3D9Device::getDeviceType() const
  447. {
  448. return mDeviceType;
  449. }
  450. //---------------------------------------------------------------------
  451. bool D3D9Device::isMultihead() const
  452. {
  453. RenderWindowToResorucesMap::const_iterator it = mMapRenderWindowToResoruces.begin();
  454. while (it != mMapRenderWindowToResoruces.end())
  455. {
  456. RenderWindowResources* renderWindowResources = it->second;
  457. if (renderWindowResources->adapterOrdinalInGroupIndex > 0 &&
  458. it->first->isFullScreen())
  459. {
  460. return true;
  461. }
  462. ++it;
  463. }
  464. return false;
  465. }
  466. //---------------------------------------------------------------------
  467. void D3D9Device::clearDeviceStreams()
  468. {
  469. D3D9RenderSystem* renderSystem = static_cast<D3D9RenderSystem*>(CamelotEngine::RenderSystemManager::getActive());
  470. // Set all texture units to nothing to release texture surfaces
  471. for (DWORD stage = 0; stage < mD3D9DeviceCaps.MaxSimultaneousTextures; ++stage)
  472. {
  473. DWORD dwCurValue = D3DTOP_FORCE_DWORD;
  474. HRESULT hr;
  475. hr = mpDevice->SetTexture(stage, NULL);
  476. if( hr != S_OK )
  477. {
  478. String str = "Unable to disable texture '" + StringConverter::toString((unsigned int)stage) + "' in D3D9";
  479. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, str, "D3D9Device::clearDeviceStreams" );
  480. }
  481. mpDevice->GetTextureStageState(static_cast<DWORD>(stage), D3DTSS_COLOROP, &dwCurValue);
  482. if (dwCurValue != D3DTOP_DISABLE)
  483. {
  484. hr = mpDevice->SetTextureStageState(static_cast<DWORD>(stage), D3DTSS_COLOROP, D3DTOP_DISABLE);
  485. if( hr != S_OK )
  486. {
  487. String str = "Unable to disable texture '" + StringConverter::toString((unsigned)stage) + "' in D3D9";
  488. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, str, "D3D9Device::clearDeviceStreams" );
  489. }
  490. }
  491. // set stage desc. to defaults
  492. renderSystem->mTexStageDesc[stage].pTex = 0;
  493. renderSystem->mTexStageDesc[stage].autoTexCoordType = TEXCALC_NONE;
  494. renderSystem->mTexStageDesc[stage].coordIndex = 0;
  495. renderSystem->mTexStageDesc[stage].texType = D3D9Mappings::D3D_TEX_TYPE_NORMAL;
  496. }
  497. // Unbind any vertex streams to avoid memory leaks
  498. for (unsigned int i = 0; i < mD3D9DeviceCaps.MaxStreams; ++i)
  499. {
  500. mpDevice->SetStreamSource(i, NULL, 0, 0);
  501. }
  502. }
  503. //---------------------------------------------------------------------
  504. void D3D9Device::createD3D9Device()
  505. {
  506. // Update focus window.
  507. D3D9RenderWindow* primaryRenderWindow = getPrimaryWindow();
  508. // Case we have to share the same focus window.
  509. if (msSharedFocusWindow != NULL)
  510. mFocusWindow = msSharedFocusWindow;
  511. else
  512. mFocusWindow = primaryRenderWindow->getWindowHandle();
  513. IDirect3D9* pD3D9 = D3D9RenderSystem::getDirect3D9();
  514. HRESULT hr;
  515. if (isMultihead())
  516. {
  517. mBehaviorFlags |= D3DCREATE_ADAPTERGROUP_DEVICE;
  518. }
  519. else
  520. {
  521. mBehaviorFlags &= ~D3DCREATE_ADAPTERGROUP_DEVICE;
  522. }
  523. // Try to create the device with hardware vertex processing.
  524. mBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
  525. hr = pD3D9->CreateDevice(mAdapterNumber, mDeviceType, mFocusWindow,
  526. mBehaviorFlags, mPresentationParams, &mpDevice);
  527. if (FAILED(hr))
  528. {
  529. // Try a second time, may fail the first time due to back buffer count,
  530. // which will be corrected down to 1 by the runtime
  531. hr = pD3D9->CreateDevice(mAdapterNumber, mDeviceType, mFocusWindow,
  532. mBehaviorFlags, mPresentationParams, &mpDevice);
  533. }
  534. // Case hardware vertex processing failed.
  535. if( FAILED( hr ) )
  536. {
  537. // Try to create the device with mixed vertex processing.
  538. mBehaviorFlags &= ~D3DCREATE_HARDWARE_VERTEXPROCESSING;
  539. mBehaviorFlags |= D3DCREATE_MIXED_VERTEXPROCESSING;
  540. hr = pD3D9->CreateDevice(mAdapterNumber, mDeviceType, mFocusWindow,
  541. mBehaviorFlags, mPresentationParams, &mpDevice);
  542. }
  543. if( FAILED( hr ) )
  544. {
  545. // try to create the device with software vertex processing.
  546. mBehaviorFlags &= ~D3DCREATE_MIXED_VERTEXPROCESSING;
  547. mBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  548. hr = pD3D9->CreateDevice(mAdapterNumber, mDeviceType, mFocusWindow,
  549. mBehaviorFlags, mPresentationParams, &mpDevice);
  550. }
  551. if ( FAILED( hr ) )
  552. {
  553. // try reference device
  554. mDeviceType = D3DDEVTYPE_REF;
  555. hr = pD3D9->CreateDevice(mAdapterNumber, mDeviceType, mFocusWindow,
  556. mBehaviorFlags, mPresentationParams, &mpDevice);
  557. if ( FAILED( hr ) )
  558. {
  559. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  560. "Cannot create device!",
  561. "D3D9Device::createD3D9Device" );
  562. }
  563. }
  564. // Get current device caps.
  565. hr = mpDevice->GetDeviceCaps(&mD3D9DeviceCaps);
  566. if( FAILED( hr ) )
  567. {
  568. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  569. "Cannot get device caps!",
  570. "D3D9Device::createD3D9Device" );
  571. }
  572. // Get current creation parameters caps.
  573. hr = mpDevice->GetCreationParameters(&mCreationParams);
  574. if ( FAILED(hr) )
  575. {
  576. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  577. "Error Get Creation Parameters",
  578. "D3D9Device:createD3D9Device" );
  579. }
  580. mD3D9DeviceCapsValid = true;
  581. // Initialize device states.
  582. setupDeviceStates();
  583. // Lock access to rendering device.
  584. D3D9RenderSystem::getResourceManager()->lockDeviceAccess();
  585. D3D9Device* pCurActiveDevice = mpDeviceManager->getActiveDevice();
  586. mpDeviceManager->setActiveDevice(this);
  587. // Inform all resources that new device created.
  588. D3D9RenderSystem::getResourceManager()->notifyOnDeviceCreate(mpDevice);
  589. mpDeviceManager->setActiveDevice(pCurActiveDevice);
  590. // UnLock access to rendering device.
  591. D3D9RenderSystem::getResourceManager()->unlockDeviceAccess();
  592. }
  593. //---------------------------------------------------------------------
  594. void D3D9Device::releaseD3D9Device()
  595. {
  596. if (mpDevice != NULL)
  597. {
  598. // Lock access to rendering device.
  599. D3D9RenderSystem::getResourceManager()->lockDeviceAccess();
  600. D3D9Device* pCurActiveDevice = mpDeviceManager->getActiveDevice();
  601. mpDeviceManager->setActiveDevice(this);
  602. // Inform all resources that device is going to be destroyed.
  603. D3D9RenderSystem::getResourceManager()->notifyOnDeviceDestroy(mpDevice);
  604. mpDeviceManager->setActiveDevice(pCurActiveDevice);
  605. clearDeviceStreams();
  606. // Release device.
  607. SAFE_RELEASE(mpDevice);
  608. // UnLock access to rendering device.
  609. D3D9RenderSystem::getResourceManager()->unlockDeviceAccess();
  610. }
  611. }
  612. //---------------------------------------------------------------------
  613. void D3D9Device::releaseRenderWindowResources(RenderWindowResources* renderWindowResources)
  614. {
  615. SAFE_RELEASE(renderWindowResources->backBuffer);
  616. SAFE_RELEASE(renderWindowResources->depthBuffer);
  617. SAFE_RELEASE(renderWindowResources->swapChain);
  618. renderWindowResources->acquired = false;
  619. }
  620. //---------------------------------------------------------------------
  621. void D3D9Device::invalidate(D3D9RenderWindow* renderWindow)
  622. {
  623. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  624. it->second->acquired = false;
  625. }
  626. //---------------------------------------------------------------------
  627. bool D3D9Device::validate(D3D9RenderWindow* renderWindow)
  628. {
  629. // Validate that the render window should run on this device.
  630. if (false == validateDisplayMonitor(renderWindow))
  631. return false;
  632. // Validate that this device created on the correct target focus window handle
  633. validateFocusWindow();
  634. // Validate that the render window dimensions matches to back buffer dimensions.
  635. validateBackBufferSize(renderWindow);
  636. // Validate that this device is in valid rendering state.
  637. if (false == validateDeviceState(renderWindow))
  638. return false;
  639. return true;
  640. }
  641. //---------------------------------------------------------------------
  642. void D3D9Device::validateFocusWindow()
  643. {
  644. // Focus window changed -> device should be re-acquired.
  645. if ((msSharedFocusWindow != NULL && mCreationParams.hFocusWindow != msSharedFocusWindow) ||
  646. (msSharedFocusWindow == NULL && mCreationParams.hFocusWindow != getPrimaryWindow()->getWindowHandle()))
  647. {
  648. // Lock access to rendering device.
  649. D3D9RenderSystem::getResourceManager()->lockDeviceAccess();
  650. release();
  651. acquire();
  652. // UnLock access to rendering device.
  653. D3D9RenderSystem::getResourceManager()->unlockDeviceAccess();
  654. }
  655. }
  656. //---------------------------------------------------------------------
  657. bool D3D9Device::validateDeviceState(D3D9RenderWindow* renderWindow)
  658. {
  659. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  660. RenderWindowResources* renderWindowResources = it->second;
  661. HRESULT hr;
  662. hr = mpDevice->TestCooperativeLevel();
  663. // Case device is not valid for rendering.
  664. if (FAILED(hr))
  665. {
  666. // device lost, and we can't reset
  667. // can't do anything about it here, wait until we get
  668. // D3DERR_DEVICENOTRESET; rendering calls will silently fail until
  669. // then (except Present, but we ignore device lost there too)
  670. if (hr == D3DERR_DEVICELOST)
  671. {
  672. releaseRenderWindowResources(renderWindowResources);
  673. notifyDeviceLost();
  674. return false;
  675. }
  676. // device lost, and we can reset
  677. else if (hr == D3DERR_DEVICENOTRESET)
  678. {
  679. bool deviceRestored = reset();
  680. // Device was not restored yet.
  681. if (deviceRestored == false)
  682. {
  683. // Wait a while
  684. Sleep(50);
  685. return false;
  686. }
  687. }
  688. }
  689. // Render window resources explicitly invalidated. (Resize or window mode switch)
  690. if (renderWindowResources->acquired == false)
  691. {
  692. if (getPrimaryWindow() == renderWindow)
  693. {
  694. bool deviceRestored = reset();
  695. // Device was not restored yet.
  696. if (deviceRestored == false)
  697. {
  698. // Wait a while
  699. Sleep(50);
  700. return false;
  701. }
  702. }
  703. else
  704. {
  705. acquire(renderWindow);
  706. }
  707. }
  708. return true;
  709. }
  710. //---------------------------------------------------------------------
  711. void D3D9Device::validateBackBufferSize(D3D9RenderWindow* renderWindow)
  712. {
  713. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  714. RenderWindowResources* renderWindowResources = it->second;
  715. // Case size has been changed.
  716. if (renderWindow->getWidth() != renderWindowResources->presentParameters.BackBufferWidth ||
  717. renderWindow->getHeight() != renderWindowResources->presentParameters.BackBufferHeight)
  718. {
  719. if (renderWindow->getWidth() > 0)
  720. renderWindowResources->presentParameters.BackBufferWidth = renderWindow->getWidth();
  721. if (renderWindow->getHeight() > 0)
  722. renderWindowResources->presentParameters.BackBufferHeight = renderWindow->getHeight();
  723. invalidate(renderWindow);
  724. }
  725. }
  726. //---------------------------------------------------------------------
  727. bool D3D9Device::validateDisplayMonitor(D3D9RenderWindow* renderWindow)
  728. {
  729. // Ignore full screen since it doesn't really move and it is possible
  730. // that it created using multi-head adapter so for a subordinate the
  731. // native monitor handle and this device handle will be different.
  732. if (renderWindow->isFullScreen())
  733. return true;
  734. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  735. HMONITOR hRenderWindowMonitor = NULL;
  736. // Find the monitor this render window belongs to.
  737. hRenderWindowMonitor = MonitorFromWindow(renderWindow->getWindowHandle(), MONITOR_DEFAULTTONULL);
  738. // This window doesn't intersect with any of the display monitor
  739. if (hRenderWindowMonitor == NULL)
  740. return false;
  741. // Case this window changed monitor.
  742. if (hRenderWindowMonitor != mMonitor)
  743. {
  744. // Lock access to rendering device.
  745. D3D9RenderSystem::getResourceManager()->lockDeviceAccess();
  746. mpDeviceManager->linkRenderWindow(renderWindow);
  747. // UnLock access to rendering device.
  748. D3D9RenderSystem::getResourceManager()->unlockDeviceAccess();
  749. return false;
  750. }
  751. return true;
  752. }
  753. //---------------------------------------------------------------------
  754. void D3D9Device::present(D3D9RenderWindow* renderWindow)
  755. {
  756. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  757. RenderWindowResources* renderWindowResources = it->second;
  758. // Skip present while current device state is invalid.
  759. if (mDeviceLost ||
  760. renderWindowResources->acquired == false ||
  761. isDeviceLost())
  762. return;
  763. HRESULT hr;
  764. if (isMultihead())
  765. {
  766. // Only the master will call present method results in synchronized
  767. // buffer swap for the rest of the implicit swap chain.
  768. if (getPrimaryWindow() == renderWindow)
  769. hr = mpDevice->Present( NULL, NULL, NULL, NULL );
  770. else
  771. hr = S_OK;
  772. }
  773. else
  774. {
  775. hr = renderWindowResources->swapChain->Present(NULL, NULL, NULL, NULL, 0);
  776. }
  777. if( D3DERR_DEVICELOST == hr)
  778. {
  779. releaseRenderWindowResources(renderWindowResources);
  780. notifyDeviceLost();
  781. }
  782. else if( FAILED(hr) )
  783. {
  784. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  785. "Error Presenting surfaces",
  786. "D3D9Device::present" );
  787. }
  788. else
  789. {
  790. // TODO PORT - What is this used for? In any case I don't have Root so just set it to 0
  791. //mLastPresentFrame = Root::getSingleton().getNextFrameNumber();
  792. mLastPresentFrame = 0;
  793. }
  794. }
  795. //---------------------------------------------------------------------
  796. void D3D9Device::acquireRenderWindowResources(RenderWindowToResorucesIterator it)
  797. {
  798. RenderWindowResources* renderWindowResources = it->second;
  799. D3D9RenderWindow* renderWindow = it->first;
  800. releaseRenderWindowResources(renderWindowResources);
  801. // Create additional swap chain
  802. if (isSwapChainWindow(renderWindow) && !isMultihead())
  803. {
  804. // Create swap chain
  805. HRESULT hr = mpDevice->CreateAdditionalSwapChain(&renderWindowResources->presentParameters,
  806. &renderWindowResources->swapChain);
  807. if (FAILED(hr))
  808. {
  809. // Try a second time, may fail the first time due to back buffer count,
  810. // which will be corrected by the runtime
  811. hr = mpDevice->CreateAdditionalSwapChain(&renderWindowResources->presentParameters,
  812. &renderWindowResources->swapChain);
  813. }
  814. if (FAILED(hr))
  815. {
  816. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  817. "Unable to create an additional swap chain",
  818. "D3D9RenderWindow::acquireRenderWindowResources");
  819. }
  820. }
  821. else
  822. {
  823. // The swap chain is already created by the device
  824. HRESULT hr = mpDevice->GetSwapChain(renderWindowResources->presentParametersIndex,
  825. &renderWindowResources->swapChain);
  826. if (FAILED(hr))
  827. {
  828. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  829. "Unable to get the swap chain",
  830. "D3D9RenderWindow::acquireRenderWindowResources");
  831. }
  832. }
  833. // Store references to buffers for convenience
  834. renderWindowResources->swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO,
  835. &renderWindowResources->backBuffer);
  836. // Additional swap chains need their own depth buffer
  837. // to support resizing them
  838. if (renderWindow->isDepthBuffered())
  839. {
  840. // if multihead is enabled, depth buffer can be created automatically for
  841. // all the adapters. if multihead is not enabled, depth buffer is just
  842. // created for the main swap chain
  843. if (isMultihead() && isAutoDepthStencil() ||
  844. isMultihead() == false && isSwapChainWindow(renderWindow) == false)
  845. {
  846. mpDevice->GetDepthStencilSurface(&renderWindowResources->depthBuffer);
  847. }
  848. else
  849. {
  850. uint targetWidth = renderWindow->getWidth();
  851. uint targetHeight = renderWindow->getHeight();
  852. if (targetWidth == 0)
  853. targetWidth = 1;
  854. if (targetHeight == 0)
  855. targetHeight = 1;
  856. HRESULT hr = mpDevice->CreateDepthStencilSurface(
  857. targetWidth, targetHeight,
  858. renderWindowResources->presentParameters.AutoDepthStencilFormat,
  859. renderWindowResources->presentParameters.MultiSampleType,
  860. renderWindowResources->presentParameters.MultiSampleQuality,
  861. (renderWindowResources->presentParameters.Flags & D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL),
  862. &renderWindowResources->depthBuffer, NULL
  863. );
  864. if (FAILED(hr))
  865. {
  866. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  867. "Unable to create a depth buffer for the swap chain",
  868. "D3D9RenderWindow::acquireRenderWindowResources");
  869. }
  870. if (isSwapChainWindow(renderWindow) == false)
  871. {
  872. mpDevice->SetDepthStencilSurface(renderWindowResources->depthBuffer);
  873. }
  874. }
  875. }
  876. renderWindowResources->acquired = true;
  877. }
  878. //---------------------------------------------------------------------
  879. void D3D9Device::setupDeviceStates()
  880. {
  881. HRESULT hr = mpDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
  882. if (FAILED(hr))
  883. {
  884. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  885. "Unable to apply render state: D3DRS_SPECULARENABLE <- TRUE",
  886. "D3D9Device::setupDeviceStates");
  887. }
  888. }
  889. //---------------------------------------------------------------------
  890. bool D3D9Device::isSwapChainWindow(D3D9RenderWindow* renderWindow)
  891. {
  892. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  893. if (it->second->presentParametersIndex == 0 || renderWindow->isFullScreen())
  894. return false;
  895. return true;
  896. }
  897. //---------------------------------------------------------------------
  898. D3D9RenderWindow* D3D9Device::getPrimaryWindow()
  899. {
  900. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
  901. while (it != mMapRenderWindowToResoruces.end() && it->second->presentParametersIndex != 0)
  902. ++it;
  903. assert(it != mMapRenderWindowToResoruces.end());
  904. return it->first;
  905. }
  906. //---------------------------------------------------------------------
  907. void D3D9Device::setSharedWindowHandle(HWND hSharedHWND)
  908. {
  909. if (hSharedHWND != msSharedFocusWindow)
  910. msSharedFocusWindow = hSharedHWND;
  911. }
  912. //---------------------------------------------------------------------
  913. void D3D9Device::updateRenderWindowsIndices()
  914. {
  915. // Update present parameters index attribute per render window.
  916. if (isMultihead())
  917. {
  918. RenderWindowToResorucesIterator it = mMapRenderWindowToResoruces.begin();
  919. // Multi head device case -
  920. // Present parameter index is based on adapter ordinal in group index.
  921. while (it != mMapRenderWindowToResoruces.end())
  922. {
  923. it->second->presentParametersIndex = it->second->adapterOrdinalInGroupIndex;
  924. ++it;
  925. }
  926. }
  927. else
  928. {
  929. // Single head device case -
  930. // Just assign index in incremental order -
  931. // NOTE: It suppose to cover both cases that possible here:
  932. // 1. Single full screen window - only one allowed per device (this is not multi-head case).
  933. // 2. Multiple window mode windows.
  934. uint nextPresParamIndex = 0;
  935. RenderWindowToResorucesIterator it;
  936. D3D9RenderWindow* deviceFocusWindow = NULL;
  937. // In case a d3d9 device exists - try to keep the present parameters order
  938. // so that the window that the device is focused on will stay the same and we
  939. // will avoid device re-creation.
  940. if (mpDevice != NULL)
  941. {
  942. it = mMapRenderWindowToResoruces.begin();
  943. while (it != mMapRenderWindowToResoruces.end())
  944. {
  945. if (it->first->getWindowHandle() == mCreationParams.hFocusWindow)
  946. {
  947. deviceFocusWindow = it->first;
  948. it->second->presentParametersIndex = nextPresParamIndex;
  949. ++nextPresParamIndex;
  950. break;
  951. }
  952. ++it;
  953. }
  954. }
  955. it = mMapRenderWindowToResoruces.begin();
  956. while (it != mMapRenderWindowToResoruces.end())
  957. {
  958. if (it->first != deviceFocusWindow)
  959. {
  960. it->second->presentParametersIndex = nextPresParamIndex;
  961. ++nextPresParamIndex;
  962. }
  963. ++it;
  964. }
  965. }
  966. }
  967. //---------------------------------------------------------------------
  968. void D3D9Device::copyContentsToMemory(D3D9RenderWindow* renderWindow,
  969. const PixelBox &dst, RenderTarget::FrameBuffer buffer)
  970. {
  971. RenderWindowToResorucesIterator it = getRenderWindowIterator(renderWindow);
  972. RenderWindowResources* resources = it->second;
  973. bool swapChain = isSwapChainWindow(renderWindow);
  974. if ((dst.left < 0) || (dst.right > renderWindow->getWidth()) ||
  975. (dst.top < 0) || (dst.bottom > renderWindow->getHeight()) ||
  976. (dst.front != 0) || (dst.back != 1))
  977. {
  978. OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
  979. "Invalid box.",
  980. "D3D9Device::copyContentsToMemory" );
  981. }
  982. HRESULT hr;
  983. IDirect3DSurface9* pSurf = NULL;
  984. IDirect3DSurface9* pTempSurf = NULL;
  985. D3DSURFACE_DESC desc;
  986. D3DLOCKED_RECT lockedRect;
  987. if (buffer == RenderTarget::FB_AUTO)
  988. {
  989. //buffer = mIsFullScreen? FB_FRONT : FB_BACK;
  990. buffer = RenderTarget::FB_FRONT;
  991. }
  992. if (buffer == RenderTarget::FB_FRONT)
  993. {
  994. D3DDISPLAYMODE dm;
  995. if (FAILED(hr = mpDevice->GetDisplayMode(0, &dm)))
  996. {
  997. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  998. "Can't get display mode: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  999. "D3D9Device::copyContentsToMemory");
  1000. }
  1001. desc.Width = dm.Width;
  1002. desc.Height = dm.Height;
  1003. desc.Format = D3DFMT_A8R8G8B8;
  1004. if (FAILED(hr = mpDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height,
  1005. desc.Format,
  1006. D3DPOOL_SYSTEMMEM,
  1007. &pTempSurf,
  1008. 0)))
  1009. {
  1010. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1011. "Can't create offscreen buffer: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1012. "D3D9Device::copyContentsToMemory");
  1013. }
  1014. if (FAILED(hr = swapChain ? resources->swapChain->GetFrontBufferData(pTempSurf) :
  1015. mpDevice->GetFrontBufferData(0, pTempSurf)))
  1016. {
  1017. SAFE_RELEASE(pTempSurf);
  1018. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1019. "Can't get front buffer: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1020. "D3D9Device::copyContentsToMemory");
  1021. }
  1022. if(renderWindow->isFullScreen())
  1023. {
  1024. if ((dst.left == 0) && (dst.right == renderWindow->getWidth()) && (dst.top == 0) && (dst.bottom == renderWindow->getHeight()))
  1025. {
  1026. hr = pTempSurf->LockRect(&lockedRect, 0, D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK);
  1027. }
  1028. else
  1029. {
  1030. RECT rect;
  1031. rect.left = static_cast<LONG>(dst.left);
  1032. rect.right = static_cast<LONG>(dst.right);
  1033. rect.top = static_cast<LONG>(dst.top);
  1034. rect.bottom = static_cast<LONG>(dst.bottom);
  1035. hr = pTempSurf->LockRect(&lockedRect, &rect, D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK);
  1036. }
  1037. if (FAILED(hr))
  1038. {
  1039. SAFE_RELEASE(pTempSurf);
  1040. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1041. "Can't lock rect: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1042. "D3D9Device::copyContentsToMemory");
  1043. }
  1044. }
  1045. else
  1046. {
  1047. RECT srcRect;
  1048. //GetClientRect(mHWnd, &srcRect);
  1049. srcRect.left = static_cast<LONG>(dst.left);
  1050. srcRect.top = static_cast<LONG>(dst.top);
  1051. srcRect.right = static_cast<LONG>(dst.right);
  1052. srcRect.bottom = static_cast<LONG>(dst.bottom);
  1053. POINT point;
  1054. point.x = srcRect.left;
  1055. point.y = srcRect.top;
  1056. ClientToScreen(renderWindow->getWindowHandle(), &point);
  1057. srcRect.top = point.y;
  1058. srcRect.left = point.x;
  1059. srcRect.bottom += point.y;
  1060. srcRect.right += point.x;
  1061. desc.Width = srcRect.right - srcRect.left;
  1062. desc.Height = srcRect.bottom - srcRect.top;
  1063. if (FAILED(hr = pTempSurf->LockRect(&lockedRect, &srcRect, D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK)))
  1064. {
  1065. SAFE_RELEASE(pTempSurf);
  1066. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1067. "Can't lock rect: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1068. "D3D9Device::copyContentsToMemory");
  1069. }
  1070. }
  1071. }
  1072. else
  1073. {
  1074. SAFE_RELEASE(pSurf);
  1075. if(FAILED(hr = swapChain? resources->swapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pSurf) :
  1076. mpDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurf)))
  1077. {
  1078. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1079. "Can't get back buffer: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1080. "D3D9Device::copyContentsToMemory");
  1081. }
  1082. if(FAILED(hr = pSurf->GetDesc(&desc)))
  1083. {
  1084. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1085. "Can't get description: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1086. "D3D9Device::copyContentsToMemory");
  1087. }
  1088. if (FAILED(hr = mpDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height,
  1089. desc.Format,
  1090. D3DPOOL_SYSTEMMEM,
  1091. &pTempSurf,
  1092. 0)))
  1093. {
  1094. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1095. "Can't create offscreen surface: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1096. "D3D9Device::copyContentsToMemory");
  1097. }
  1098. if (desc.MultiSampleType == D3DMULTISAMPLE_NONE)
  1099. {
  1100. if (FAILED(hr = mpDevice->GetRenderTargetData(pSurf, pTempSurf)))
  1101. {
  1102. SAFE_RELEASE(pTempSurf);
  1103. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1104. "Can't get render target data: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1105. "D3D9Device::copyContentsToMemory");
  1106. }
  1107. }
  1108. else
  1109. {
  1110. IDirect3DSurface9* pStretchSurf = 0;
  1111. if (FAILED(hr = mpDevice->CreateRenderTarget(desc.Width, desc.Height,
  1112. desc.Format,
  1113. D3DMULTISAMPLE_NONE,
  1114. 0,
  1115. false,
  1116. &pStretchSurf,
  1117. 0)))
  1118. {
  1119. SAFE_RELEASE(pTempSurf);
  1120. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1121. "Can't create render target: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1122. "D3D9Device::copyContentsToMemory");
  1123. }
  1124. if (FAILED(hr = mpDevice->StretchRect(pSurf, 0, pStretchSurf, 0, D3DTEXF_NONE)))
  1125. {
  1126. SAFE_RELEASE(pTempSurf);
  1127. SAFE_RELEASE(pStretchSurf);
  1128. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1129. "Can't stretch rect: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1130. "D3D9Device::copyContentsToMemory");
  1131. }
  1132. if (FAILED(hr = mpDevice->GetRenderTargetData(pStretchSurf, pTempSurf)))
  1133. {
  1134. SAFE_RELEASE(pTempSurf);
  1135. SAFE_RELEASE(pStretchSurf);
  1136. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1137. "Can't get render target data: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1138. "D3D9Device::copyContentsToMemory");
  1139. }
  1140. SAFE_RELEASE(pStretchSurf);
  1141. }
  1142. if ((dst.left == 0) && (dst.right == renderWindow->getWidth()) && (dst.top == 0) && (dst.bottom == renderWindow->getHeight()))
  1143. {
  1144. hr = pTempSurf->LockRect(&lockedRect, 0, D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK);
  1145. }
  1146. else
  1147. {
  1148. RECT rect;
  1149. rect.left = static_cast<LONG>(dst.left);
  1150. rect.right = static_cast<LONG>(dst.right);
  1151. rect.top = static_cast<LONG>(dst.top);
  1152. rect.bottom = static_cast<LONG>(dst.bottom);
  1153. hr = pTempSurf->LockRect(&lockedRect, &rect, D3DLOCK_READONLY | D3DLOCK_NOSYSLOCK);
  1154. }
  1155. if (FAILED(hr))
  1156. {
  1157. SAFE_RELEASE(pTempSurf);
  1158. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1159. "Can't lock rect: TODO PORT NO ERROR", // TODO PORT - Translate HR to error
  1160. "D3D9Device::copyContentsToMemory");
  1161. }
  1162. }
  1163. PixelFormat format = Ogre::D3D9Mappings::_getPF(desc.Format);
  1164. if (format == PF_UNKNOWN)
  1165. {
  1166. SAFE_RELEASE(pTempSurf);
  1167. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
  1168. "Unsupported format", "D3D9Device::copyContentsToMemory");
  1169. }
  1170. PixelBox src(dst.getWidth(), dst.getHeight(), 1, format, lockedRect.pBits);
  1171. src.rowPitch = lockedRect.Pitch / PixelUtil::getNumElemBytes(format);
  1172. src.slicePitch = desc.Height * src.rowPitch;
  1173. PixelUtil::bulkPixelConversion(src, dst);
  1174. SAFE_RELEASE(pTempSurf);
  1175. SAFE_RELEASE(pSurf);
  1176. }
  1177. }