BsD3D9Device.cpp 36 KB

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