BsD3D9Device.cpp 36 KB

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