CmD3D9Texture.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  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 "CmD3D9Texture.h"
  25. #include "CmD3D9HardwarePixelBuffer.h"
  26. #include "CmException.h"
  27. #include "CmBitwise.h"
  28. #include "CmD3D9Mappings.h"
  29. #include "CmD3D9RenderSystem.h"
  30. #include "CmD3D9TextureManager.h"
  31. #include "CmD3D9Device.h"
  32. #include "CmD3D9DeviceManager.h"
  33. #include "CmD3D9ResourceManager.h"
  34. #if CM_DEBUG_MODE
  35. #define THROW_IF_NOT_RENDER_THREAD throwIfNotRenderThread();
  36. #else
  37. #define THROW_IF_NOT_RENDER_THREAD
  38. #endif
  39. namespace CamelotEngine
  40. {
  41. /****************************************************************************************/
  42. D3D9Texture::D3D9Texture()
  43. :Texture(),
  44. mD3DPool(D3DPOOL_MANAGED),
  45. mDynamicTextures(false),
  46. mHwGammaReadSupported(false)
  47. {
  48. }
  49. /****************************************************************************************/
  50. D3D9Texture::~D3D9Texture()
  51. {
  52. THROW_IF_NOT_RENDER_THREAD;
  53. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  54. // have to call this here reather than in Resource destructor
  55. // since calling virtual methods in base destructors causes crash
  56. freeInternalResources();
  57. // Free memory allocated per device.
  58. DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.begin();
  59. while (it != mMapDeviceToTextureResources.end())
  60. {
  61. TextureResources* textureResource = it->second;
  62. SAFE_DELETE(textureResource);
  63. ++it;
  64. }
  65. mMapDeviceToTextureResources.clear();
  66. mSurfaceList.clear();
  67. }
  68. /****************************************************************************************/
  69. PixelData D3D9Texture::lockImpl(LockOptions options, UINT32 mipLevel, UINT32 face)
  70. {
  71. if(mLockedBuffer != nullptr)
  72. CM_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  73. UINT32 mipWidth = mipLevel >> mWidth;
  74. UINT32 mipHeight = mipLevel >> mHeight;
  75. UINT32 mipDepth = mipLevel >> mDepth;
  76. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
  77. mLockedBuffer = getBuffer(face, mipLevel);
  78. lockedArea.data = mLockedBuffer->lock(options);
  79. return lockedArea;
  80. }
  81. /****************************************************************************************/
  82. void D3D9Texture::unlockImpl()
  83. {
  84. if(mLockedBuffer == nullptr)
  85. CM_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  86. mLockedBuffer->unlock();
  87. mLockedBuffer = nullptr;
  88. }
  89. /****************************************************************************************/
  90. void D3D9Texture::copyImpl(TexturePtr& target)
  91. {
  92. THROW_IF_NOT_RENDER_THREAD
  93. // check if this & target are the same format and type
  94. // blitting from or to cube textures is not supported yet
  95. if (target->getUsage() != getUsage() ||
  96. target->getTextureType() != getTextureType())
  97. {
  98. CM_EXCEPT(InvalidParametersException,
  99. "Src. and dest. textures must be of same type and must have the same usage !!!");
  100. }
  101. HRESULT hr;
  102. D3D9Texture *other;
  103. // get the target
  104. other = reinterpret_cast< D3D9Texture * >( target.get() );
  105. // target rectangle (whole surface)
  106. RECT dstRC = {0, 0, static_cast<LONG>(other->getWidth()), static_cast<LONG>(other->getHeight())};
  107. DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.begin();
  108. while (it != mMapDeviceToTextureResources.end())
  109. {
  110. TextureResources* srcTextureResource = it->second;
  111. TextureResources* dstTextureResource = other->getTextureResources(it->first);
  112. // do it plain for normal texture
  113. if (getTextureType() == TEX_TYPE_2D &&
  114. srcTextureResource->pNormTex &&
  115. dstTextureResource->pNormTex)
  116. {
  117. // get our source surface
  118. IDirect3DSurface9 *pSrcSurface = 0;
  119. if( FAILED( hr = srcTextureResource->pNormTex->GetSurfaceLevel(0, &pSrcSurface) ) )
  120. {
  121. String msg = DXGetErrorDescription(hr);
  122. CM_EXCEPT(RenderingAPIException, "Couldn't blit : " + msg);
  123. }
  124. // get our target surface
  125. IDirect3DSurface9 *pDstSurface = 0;
  126. if( FAILED( hr = dstTextureResource->pNormTex->GetSurfaceLevel(0, &pDstSurface) ) )
  127. {
  128. String msg = DXGetErrorDescription(hr);
  129. SAFE_RELEASE(pSrcSurface);
  130. CM_EXCEPT(RenderingAPIException, "Couldn't blit : " + msg );
  131. }
  132. // do the blit, it's called StretchRect in D3D9 :)
  133. if( FAILED( hr = it->first->StretchRect( pSrcSurface, NULL, pDstSurface, &dstRC, D3DTEXF_NONE) ) )
  134. {
  135. String msg = DXGetErrorDescription(hr);
  136. SAFE_RELEASE(pSrcSurface);
  137. SAFE_RELEASE(pDstSurface);
  138. CM_EXCEPT(RenderingAPIException, "Couldn't blit : " + msg);
  139. }
  140. // release temp. surfaces
  141. SAFE_RELEASE(pSrcSurface);
  142. SAFE_RELEASE(pDstSurface);
  143. }
  144. else if (getTextureType() == TEX_TYPE_CUBE_MAP &&
  145. srcTextureResource->pCubeTex &&
  146. dstTextureResource->pCubeTex)
  147. {
  148. // blit to 6 cube faces
  149. for (UINT32 face = 0; face < 6; face++)
  150. {
  151. // get our source surface
  152. IDirect3DSurface9 *pSrcSurface = 0;
  153. if( FAILED( hr =srcTextureResource->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, 0, &pSrcSurface) ) )
  154. {
  155. String msg = DXGetErrorDescription(hr);
  156. CM_EXCEPT(RenderingAPIException, "Couldn't blit : " + msg);
  157. }
  158. // get our target surface
  159. IDirect3DSurface9 *pDstSurface = 0;
  160. if( FAILED( hr = dstTextureResource->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, 0, &pDstSurface) ) )
  161. {
  162. String msg = DXGetErrorDescription(hr);
  163. SAFE_RELEASE(pSrcSurface);
  164. CM_EXCEPT(RenderingAPIException, "Couldn't blit : " + msg);
  165. }
  166. // do the blit, it's called StretchRect in D3D9 :)
  167. if( FAILED( hr = it->first->StretchRect( pSrcSurface, NULL, pDstSurface, &dstRC, D3DTEXF_NONE) ) )
  168. {
  169. String msg = DXGetErrorDescription(hr);
  170. SAFE_RELEASE(pSrcSurface);
  171. SAFE_RELEASE(pDstSurface);
  172. CM_EXCEPT(RenderingAPIException, "Couldn't blit : " + msg);
  173. }
  174. // release temp. surfaces
  175. SAFE_RELEASE(pSrcSurface);
  176. SAFE_RELEASE(pDstSurface);
  177. }
  178. }
  179. else
  180. {
  181. CM_EXCEPT(NotImplementedException, "Copy to texture is implemented only for 2D and cube textures !!!");
  182. }
  183. ++it;
  184. }
  185. }
  186. /****************************************************************************************/
  187. void D3D9Texture::initialize_internal()
  188. {
  189. THROW_IF_NOT_RENDER_THREAD
  190. createInternalResources();
  191. Resource::initialize_internal();
  192. }
  193. /****************************************************************************************/
  194. void D3D9Texture::freeInternalResourcesImpl()
  195. {
  196. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  197. DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.begin();
  198. while (it != mMapDeviceToTextureResources.end())
  199. {
  200. TextureResources* textureResource = it->second;
  201. freeTextureResources(it->first, textureResource);
  202. ++it;
  203. }
  204. }
  205. /****************************************************************************************/
  206. D3D9Texture::TextureResources* D3D9Texture::getTextureResources(IDirect3DDevice9* d3d9Device)
  207. {
  208. DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.find(d3d9Device);
  209. if (it == mMapDeviceToTextureResources.end())
  210. return NULL;
  211. return it->second;
  212. }
  213. /****************************************************************************************/
  214. D3D9Texture::TextureResources* D3D9Texture::allocateTextureResources(IDirect3DDevice9* d3d9Device)
  215. {
  216. assert(mMapDeviceToTextureResources.find(d3d9Device) == mMapDeviceToTextureResources.end());
  217. TextureResources* textureResources = new TextureResources;
  218. textureResources->pNormTex = NULL;
  219. textureResources->pCubeTex = NULL;
  220. textureResources->pVolumeTex = NULL;
  221. textureResources->pBaseTex = NULL;
  222. mMapDeviceToTextureResources[d3d9Device] = textureResources;
  223. return textureResources;
  224. }
  225. /****************************************************************************************/
  226. void D3D9Texture::createTextureResources(IDirect3DDevice9* d3d9Device)
  227. {
  228. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  229. /* prepareImpl();
  230. loadImpl(d3d9Device);
  231. postLoadImpl(); */
  232. }
  233. /****************************************************************************************/
  234. void D3D9Texture::freeTextureResources(IDirect3DDevice9* d3d9Device, D3D9Texture::TextureResources* textureResources)
  235. {
  236. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  237. // Release surfaces from each mip level.
  238. for(unsigned int i = 0; i < mSurfaceList.size(); ++i)
  239. {
  240. D3D9HardwarePixelBuffer* pixelBuffer = static_cast<D3D9HardwarePixelBuffer*>(mSurfaceList[i].get());
  241. pixelBuffer->releaseSurfaces(d3d9Device);
  242. }
  243. // Release the rest of the resources.
  244. SAFE_RELEASE(textureResources->pBaseTex);
  245. SAFE_RELEASE(textureResources->pNormTex);
  246. SAFE_RELEASE(textureResources->pCubeTex);
  247. SAFE_RELEASE(textureResources->pVolumeTex);
  248. }
  249. /****************************************************************************************/
  250. UINT32 D3D9Texture::calculateSize(void) const
  251. {
  252. UINT32 instanceSize = getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  253. return instanceSize * (UINT32)mMapDeviceToTextureResources.size();
  254. }
  255. /****************************************************************************************/
  256. void D3D9Texture::determinePool()
  257. {
  258. if (useDefaultPool())
  259. {
  260. mD3DPool = D3DPOOL_DEFAULT;
  261. }
  262. else
  263. {
  264. mD3DPool = D3DPOOL_MANAGED;
  265. }
  266. }
  267. /****************************************************************************************/
  268. void D3D9Texture::createInternalResourcesImpl(void)
  269. {
  270. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  271. for (UINT32 i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  272. {
  273. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  274. createInternalResourcesImpl(d3d9Device);
  275. }
  276. }
  277. /****************************************************************************************/
  278. void D3D9Texture::createInternalResourcesImpl(IDirect3DDevice9* d3d9Device)
  279. {
  280. TextureResources* textureResources;
  281. // Check if resources already exist.
  282. textureResources = getTextureResources(d3d9Device);
  283. if (textureResources != NULL && textureResources->pBaseTex != NULL)
  284. return;
  285. // load based on tex.type
  286. switch (getTextureType())
  287. {
  288. case TEX_TYPE_1D:
  289. case TEX_TYPE_2D:
  290. _createNormTex(d3d9Device);
  291. break;
  292. case TEX_TYPE_CUBE_MAP:
  293. _createCubeTex(d3d9Device);
  294. break;
  295. case TEX_TYPE_3D:
  296. _createVolumeTex(d3d9Device);
  297. break;
  298. default:
  299. freeInternalResources();
  300. CM_EXCEPT(InternalErrorException, "Unknown texture type");
  301. }
  302. }
  303. /****************************************************************************************/
  304. void D3D9Texture::_createNormTex(IDirect3DDevice9* d3d9Device)
  305. {
  306. // we must have those defined here
  307. assert(mWidth > 0 || mHeight > 0);
  308. // determine wich D3D9 pixel format we'll use
  309. HRESULT hr;
  310. D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
  311. // let's D3DX check the corrected pixel format
  312. hr = D3DXCheckTextureRequirements(d3d9Device, NULL, NULL, NULL, 0, &d3dPF, mD3DPool);
  313. // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
  314. DWORD usage = 0;
  315. UINT numMips = (mNumMipmaps == MIP_UNLIMITED) ?
  316. D3DX_DEFAULT : mNumMipmaps + 1;
  317. // Check dynamic textures
  318. if (mUsage & TU_DYNAMIC)
  319. {
  320. if (_canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_TEXTURE, d3dPF))
  321. {
  322. usage |= D3DUSAGE_DYNAMIC;
  323. mDynamicTextures = true;
  324. }
  325. else
  326. {
  327. mDynamicTextures = false;
  328. }
  329. }
  330. // Check sRGB support
  331. if (mHwGamma)
  332. {
  333. mHwGammaReadSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_TEXTURE, d3dPF, false);
  334. }
  335. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  336. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  337. // check if mip maps are supported on hardware
  338. if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPMAP))
  339. {
  340. // no mip map support for this kind of textures :(
  341. mNumMipmaps = 0;
  342. numMips = 1;
  343. }
  344. // derive the pool to use
  345. determinePool();
  346. TextureResources* textureResources;
  347. // Get or create new texture resources structure.
  348. textureResources = getTextureResources(d3d9Device);
  349. if (textureResources != NULL)
  350. freeTextureResources(d3d9Device, textureResources);
  351. else
  352. textureResources = allocateTextureResources(d3d9Device);
  353. // create the texture
  354. hr = D3DXCreateTexture(
  355. d3d9Device, // device
  356. static_cast<UINT>(mWidth), // width
  357. static_cast<UINT>(mHeight), // height
  358. numMips, // number of mip map levels
  359. usage, // usage
  360. d3dPF, // pixel format
  361. mD3DPool,
  362. &textureResources->pNormTex); // data pointer
  363. // check result and except if failed
  364. if (FAILED(hr))
  365. {
  366. freeInternalResources();
  367. CM_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
  368. }
  369. // set the base texture we'll use in the render system
  370. hr = textureResources->pNormTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
  371. if (FAILED(hr))
  372. {
  373. freeInternalResources();
  374. CM_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
  375. }
  376. // set final tex. attributes from tex. description
  377. // they may differ from the source image !!!
  378. D3DSURFACE_DESC desc;
  379. hr = textureResources->pNormTex->GetLevelDesc(0, &desc);
  380. if (FAILED(hr))
  381. {
  382. freeInternalResources();
  383. CM_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  384. }
  385. _setFinalAttributes(d3d9Device, textureResources,
  386. desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
  387. }
  388. /****************************************************************************************/
  389. void D3D9Texture::_createCubeTex(IDirect3DDevice9* d3d9Device)
  390. {
  391. // we must have those defined here
  392. assert(mWidth > 0 || mHeight > 0);
  393. // determine wich D3D9 pixel format we'll use
  394. HRESULT hr;
  395. D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
  396. // let's D3DX check the corrected pixel format
  397. hr = D3DXCheckCubeTextureRequirements(d3d9Device, NULL, NULL, 0, &d3dPF, mD3DPool);
  398. // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
  399. DWORD usage = 0;
  400. UINT numMips = (mNumMipmaps == MIP_UNLIMITED) ?
  401. D3DX_DEFAULT : mNumMipmaps + 1;
  402. // Check dynamic textures
  403. if (mUsage & TU_DYNAMIC)
  404. {
  405. if (_canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF))
  406. {
  407. usage |= D3DUSAGE_DYNAMIC;
  408. mDynamicTextures = true;
  409. }
  410. else
  411. {
  412. mDynamicTextures = false;
  413. }
  414. }
  415. // Check sRGB support
  416. if (mHwGamma)
  417. {
  418. mHwGammaReadSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF, false);
  419. }
  420. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  421. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  422. // check if mip map cube textures are supported
  423. if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
  424. {
  425. // no mip map support for this kind of textures :(
  426. mNumMipmaps = 0;
  427. numMips = 1;
  428. }
  429. // derive the pool to use
  430. determinePool();
  431. TextureResources* textureResources;
  432. // Get or create new texture resources structure.
  433. textureResources = getTextureResources(d3d9Device);
  434. if (textureResources != NULL)
  435. freeTextureResources(d3d9Device, textureResources);
  436. else
  437. textureResources = allocateTextureResources(d3d9Device);
  438. // create the texture
  439. hr = D3DXCreateCubeTexture(
  440. d3d9Device, // device
  441. static_cast<UINT>(mWidth), // dimension
  442. numMips, // number of mip map levels
  443. usage, // usage
  444. d3dPF, // pixel format
  445. mD3DPool,
  446. &textureResources->pCubeTex); // data pointer
  447. // check result and except if failed
  448. if (FAILED(hr))
  449. {
  450. freeInternalResources();
  451. CM_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
  452. }
  453. // set the base texture we'll use in the render system
  454. hr = textureResources->pCubeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
  455. if (FAILED(hr))
  456. {
  457. freeInternalResources();
  458. CM_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
  459. }
  460. // set final tex. attributes from tex. description
  461. // they may differ from the source image !!!
  462. D3DSURFACE_DESC desc;
  463. hr = textureResources->pCubeTex->GetLevelDesc(0, &desc);
  464. if (FAILED(hr))
  465. {
  466. freeInternalResources();
  467. CM_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  468. }
  469. _setFinalAttributes(d3d9Device, textureResources,
  470. desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
  471. }
  472. /****************************************************************************************/
  473. void D3D9Texture::_createVolumeTex(IDirect3DDevice9* d3d9Device)
  474. {
  475. // we must have those defined here
  476. assert(mWidth > 0 && mHeight > 0 && mDepth>0);
  477. // determine which D3D9 pixel format we'll use
  478. HRESULT hr;
  479. D3DFORMAT d3dPF = _chooseD3DFormat(d3d9Device);
  480. // let's D3DX check the corrected pixel format
  481. hr = D3DXCheckVolumeTextureRequirements(d3d9Device, NULL, NULL, NULL, NULL, 0, &d3dPF, mD3DPool);
  482. // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
  483. DWORD usage = 0;
  484. UINT numMips = (mNumMipmaps == MIP_UNLIMITED) ?
  485. D3DX_DEFAULT : mNumMipmaps + 1;
  486. // Check dynamic textures
  487. if (mUsage & TU_DYNAMIC)
  488. {
  489. if (_canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF))
  490. {
  491. usage |= D3DUSAGE_DYNAMIC;
  492. mDynamicTextures = true;
  493. }
  494. else
  495. {
  496. mDynamicTextures = false;
  497. }
  498. }
  499. // Check sRGB support
  500. if (mHwGamma)
  501. mHwGammaReadSupported = _canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF, false);
  502. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  503. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  504. // check if mip map volume textures are supported
  505. if (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
  506. {
  507. // no mip map support for this kind of textures :(
  508. mNumMipmaps = 0;
  509. numMips = 1;
  510. }
  511. // derive the pool to use
  512. determinePool();
  513. TextureResources* textureResources;
  514. // Get or create new texture resources structure.
  515. textureResources = getTextureResources(d3d9Device);
  516. if (textureResources != NULL)
  517. freeTextureResources(d3d9Device, textureResources);
  518. else
  519. textureResources = allocateTextureResources(d3d9Device);
  520. // create the texture
  521. hr = D3DXCreateVolumeTexture(
  522. d3d9Device, // device
  523. static_cast<UINT>(mWidth), // dimension
  524. static_cast<UINT>(mHeight),
  525. static_cast<UINT>(mDepth),
  526. numMips, // number of mip map levels
  527. usage, // usage
  528. d3dPF, // pixel format
  529. mD3DPool,
  530. &textureResources->pVolumeTex); // data pointer
  531. // check result and except if failed
  532. if (FAILED(hr))
  533. {
  534. freeInternalResources();
  535. CM_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
  536. }
  537. // set the base texture we'll use in the render system
  538. hr = textureResources->pVolumeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
  539. if (FAILED(hr))
  540. {
  541. freeInternalResources();
  542. CM_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
  543. }
  544. // set final tex. attributes from tex. description
  545. // they may differ from the source image !!!
  546. D3DVOLUME_DESC desc;
  547. hr = textureResources->pVolumeTex->GetLevelDesc(0, &desc);
  548. if (FAILED(hr))
  549. {
  550. freeInternalResources();
  551. CM_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  552. }
  553. _setFinalAttributes(d3d9Device, textureResources,
  554. desc.Width, desc.Height, desc.Depth, D3D9Mappings::_getPF(desc.Format));
  555. }
  556. /****************************************************************************************/
  557. void D3D9Texture::_setFinalAttributes(IDirect3DDevice9* d3d9Device,
  558. TextureResources* textureResources,
  559. unsigned long width, unsigned long height,
  560. unsigned long depth, PixelFormat format)
  561. {
  562. if(width != mWidth || height != mHeight || depth != mDepth)
  563. {
  564. CM_EXCEPT(InternalErrorException, "Wanted and created textures sizes don't match!" \
  565. "Width: " + toString(width) + "/" + toString(mWidth) +
  566. "Height: " + toString(height) + "/" + toString(mHeight) +
  567. "Depth: " + toString(depth) + "/" + toString(mDepth));
  568. }
  569. if(mFormat != format)
  570. {
  571. CM_EXCEPT(InternalErrorException, "Wanted and created texture format doesn't match: " +
  572. toString(format) + "/" + toString(mFormat));
  573. }
  574. // Create list of subsurfaces for getBuffer()
  575. _createSurfaceList(d3d9Device, textureResources);
  576. }
  577. /****************************************************************************************/
  578. D3DTEXTUREFILTERTYPE D3D9Texture::_getBestFilterMethod(IDirect3DDevice9* d3d9Device)
  579. {
  580. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  581. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  582. DWORD filterCaps = 0;
  583. // Minification filter is used for mipmap generation
  584. // Pick the best one supported for this tex type
  585. switch (getTextureType())
  586. {
  587. case TEX_TYPE_1D: // Same as 2D
  588. case TEX_TYPE_2D: filterCaps = rkCurCaps.TextureFilterCaps; break;
  589. case TEX_TYPE_3D: filterCaps = rkCurCaps.VolumeTextureFilterCaps; break;
  590. case TEX_TYPE_CUBE_MAP: filterCaps = rkCurCaps.CubeTextureFilterCaps; break;
  591. }
  592. if(filterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
  593. return D3DTEXF_GAUSSIANQUAD;
  594. if(filterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
  595. return D3DTEXF_PYRAMIDALQUAD;
  596. if(filterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
  597. return D3DTEXF_ANISOTROPIC;
  598. if(filterCaps & D3DPTFILTERCAPS_MINFLINEAR)
  599. return D3DTEXF_LINEAR;
  600. if(filterCaps & D3DPTFILTERCAPS_MINFPOINT)
  601. return D3DTEXF_POINT;
  602. return D3DTEXF_POINT;
  603. }
  604. /****************************************************************************************/
  605. bool D3D9Texture::_canUseDynamicTextures(IDirect3DDevice9* d3d9Device,
  606. DWORD srcUsage,
  607. D3DRESOURCETYPE srcType,
  608. D3DFORMAT srcFormat)
  609. {
  610. HRESULT hr;
  611. IDirect3D9* pD3D = NULL;
  612. hr = d3d9Device->GetDirect3D(&pD3D);
  613. if (FAILED(hr))
  614. {
  615. CM_EXCEPT(InvalidParametersException, "GetDirect3D failed !!!");
  616. }
  617. if (pD3D != NULL)
  618. pD3D->Release();
  619. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  620. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  621. D3DFORMAT eBackBufferFormat = device->getBackBufferFormat();
  622. // Check for dynamic texture support
  623. // check for auto gen. mip maps support
  624. hr = pD3D->CheckDeviceFormat(
  625. rkCurCaps.AdapterOrdinal,
  626. rkCurCaps.DeviceType,
  627. eBackBufferFormat,
  628. srcUsage | D3DUSAGE_DYNAMIC,
  629. srcType,
  630. srcFormat);
  631. if (hr == D3D_OK)
  632. return true;
  633. else
  634. return false;
  635. }
  636. /****************************************************************************************/
  637. bool D3D9Texture::_canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device,
  638. DWORD srcUsage,
  639. D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting)
  640. {
  641. HRESULT hr;
  642. IDirect3D9* pD3D = NULL;
  643. hr = d3d9Device->GetDirect3D(&pD3D);
  644. if (FAILED(hr))
  645. {
  646. CM_EXCEPT(InvalidParametersException, "GetDirect3D failed !!!" );
  647. }
  648. if (pD3D != NULL)
  649. pD3D->Release();
  650. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  651. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  652. D3DFORMAT eBackBufferFormat = device->getBackBufferFormat();
  653. // Always check 'read' capability here
  654. // We will check 'write' capability only in the context of a render target
  655. if (forwriting)
  656. srcUsage |= D3DUSAGE_QUERY_SRGBWRITE;
  657. else
  658. srcUsage |= D3DUSAGE_QUERY_SRGBREAD;
  659. // Check for sRGB support
  660. // check for auto gen. mip maps support
  661. hr = pD3D->CheckDeviceFormat(
  662. rkCurCaps.AdapterOrdinal,
  663. rkCurCaps.DeviceType,
  664. eBackBufferFormat,
  665. srcUsage,
  666. srcType,
  667. srcFormat);
  668. if (hr == D3D_OK)
  669. return true;
  670. else
  671. return false;
  672. }
  673. /****************************************************************************************/
  674. bool D3D9Texture::_canAutoGenMipmaps(IDirect3DDevice9* d3d9Device,
  675. DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat)
  676. {
  677. HRESULT hr;
  678. IDirect3D9* pD3D = NULL;
  679. hr = d3d9Device->GetDirect3D(&pD3D);
  680. if (FAILED(hr))
  681. {
  682. CM_EXCEPT(InvalidParametersException, "GetDirect3D failed !!!");
  683. }
  684. if (pD3D != NULL)
  685. pD3D->Release();
  686. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  687. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  688. D3DFORMAT eBackBufferFormat = device->getBackBufferFormat();
  689. // Hacky override - many (all?) cards seem to not be able to autogen on
  690. // textures which are not a power of two
  691. // Can we even mipmap on 3D textures? Well
  692. if ((mWidth & mWidth-1) || (mHeight & mHeight-1) || (mDepth & mDepth-1))
  693. return false;
  694. if (rkCurCaps.Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
  695. {
  696. HRESULT hr;
  697. // check for auto gen. mip maps support
  698. hr = pD3D->CheckDeviceFormat(
  699. rkCurCaps.AdapterOrdinal,
  700. rkCurCaps.DeviceType,
  701. eBackBufferFormat,
  702. srcUsage | D3DUSAGE_AUTOGENMIPMAP,
  703. srcType,
  704. srcFormat);
  705. // this HR could a SUCCES
  706. // but mip maps will not be generated
  707. if (hr == D3D_OK)
  708. return true;
  709. else
  710. return false;
  711. }
  712. else
  713. return false;
  714. }
  715. /****************************************************************************************/
  716. D3DFORMAT D3D9Texture::_chooseD3DFormat(IDirect3DDevice9* d3d9Device)
  717. {
  718. // Choose frame buffer pixel format in case PF_UNKNOWN was requested
  719. if(mFormat == PF_UNKNOWN)
  720. {
  721. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  722. return device->getBackBufferFormat();
  723. }
  724. // Choose closest supported D3D format as a D3D format
  725. return D3D9Mappings::_getPF(D3D9Mappings::_getClosestSupportedPF(mFormat));
  726. }
  727. /****************************************************************************************/
  728. // Macro to hide ugly cast
  729. #define GETLEVEL(face,mip) \
  730. static_cast<D3D9HardwarePixelBuffer*>(mSurfaceList[face*(mNumMipmaps+1)+mip].get())
  731. void D3D9Texture::_createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources)
  732. {
  733. IDirect3DSurface9 *surface;
  734. IDirect3DVolume9 *volume;
  735. D3D9HardwarePixelBuffer *buffer;
  736. UINT32 mip, face;
  737. assert(textureResources != NULL);
  738. assert(textureResources->pBaseTex);
  739. // Make sure number of mips is right
  740. UINT32 numCreatedMips = textureResources->pBaseTex->GetLevelCount() - 1;
  741. if(numCreatedMips != mNumMipmaps)
  742. {
  743. CM_EXCEPT(InternalErrorException, "Number of created and wanted mip map levels doesn't match: " +
  744. toString(numCreatedMips) + "/" + toString(mNumMipmaps));
  745. }
  746. // Need to know static / dynamic
  747. unsigned int bufusage;
  748. if ((mUsage & TU_DYNAMIC) && mDynamicTextures)
  749. {
  750. bufusage = HardwareBuffer::HBU_DYNAMIC;
  751. }
  752. else
  753. {
  754. bufusage = HardwareBuffer::HBU_STATIC;
  755. }
  756. UINT32 surfaceCount = static_cast<UINT32>((getNumFaces() * (mNumMipmaps + 1)));
  757. bool updateOldList = mSurfaceList.size() == surfaceCount;
  758. if(!updateOldList)
  759. {
  760. // Create new list of surfaces
  761. mSurfaceList.clear();
  762. for(UINT32 face=0; face<getNumFaces(); ++face)
  763. {
  764. for(UINT32 mip=0; mip<=mNumMipmaps; ++mip)
  765. {
  766. buffer = new D3D9HardwarePixelBuffer((HardwareBuffer::Usage)bufusage, this);
  767. mSurfaceList.push_back(HardwarePixelBufferPtr(buffer));
  768. }
  769. }
  770. }
  771. switch(getTextureType()) {
  772. case TEX_TYPE_2D:
  773. case TEX_TYPE_1D:
  774. assert(textureResources->pNormTex);
  775. // For all mipmaps, store surfaces as HardwarePixelBufferPtr
  776. for(mip=0; mip<=mNumMipmaps; ++mip)
  777. {
  778. if(textureResources->pNormTex->GetSurfaceLevel(static_cast<UINT>(mip), &surface) != D3D_OK)
  779. CM_EXCEPT(RenderingAPIException, "Get surface level failed");
  780. D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, mip);
  781. currPixelBuffer->bind(d3d9Device, surface, nullptr, false, 0, "PortNoName", textureResources->pBaseTex);
  782. // decrement reference count, the GetSurfaceLevel call increments this
  783. // this is safe because the pixel buffer keeps a reference as well
  784. surface->Release();
  785. }
  786. break;
  787. case TEX_TYPE_CUBE_MAP:
  788. assert(textureResources->pCubeTex);
  789. // For all faces and mipmaps, store surfaces as HardwarePixelBufferPtr
  790. for(face=0; face<6; ++face)
  791. {
  792. for(mip=0; mip<=mNumMipmaps; ++mip)
  793. {
  794. if(textureResources->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, static_cast<UINT>(mip), &surface) != D3D_OK)
  795. CM_EXCEPT(RenderingAPIException, "Get cubemap surface failed");
  796. D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(face, mip);
  797. currPixelBuffer->bind(d3d9Device, surface, nullptr, false, 0, "NoNamePort", textureResources->pBaseTex);
  798. // decrement reference count, the GetSurfaceLevel call increments this
  799. // this is safe because the pixel buffer keeps a reference as well
  800. surface->Release();
  801. }
  802. }
  803. break;
  804. case TEX_TYPE_3D:
  805. assert(textureResources->pVolumeTex);
  806. // For all mipmaps, store surfaces as HardwarePixelBufferPtr
  807. for(mip=0; mip<=mNumMipmaps; ++mip)
  808. {
  809. if(textureResources->pVolumeTex->GetVolumeLevel(static_cast<UINT>(mip), &volume) != D3D_OK)
  810. CM_EXCEPT(RenderingAPIException, "Get volume level failed");
  811. D3D9HardwarePixelBuffer* currPixelBuffer = GETLEVEL(0, mip);
  812. currPixelBuffer->bind(d3d9Device, volume, textureResources->pBaseTex);
  813. // decrement reference count, the GetSurfaceLevel call increments this
  814. // this is safe because the pixel buffer keeps a reference as well
  815. volume->Release();
  816. }
  817. break;
  818. };
  819. }
  820. #undef GETLEVEL
  821. /****************************************************************************************/
  822. HardwarePixelBufferPtr D3D9Texture::getBuffer(UINT32 face, UINT32 mipmap)
  823. {
  824. THROW_IF_NOT_RENDER_THREAD;
  825. if(face >= getNumFaces())
  826. CM_EXCEPT(InvalidParametersException, "A three dimensional cube has six faces");
  827. if(mipmap > mNumMipmaps)
  828. CM_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  829. UINT32 idx = face*(mNumMipmaps+1) + mipmap;
  830. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  831. TextureResources* textureResources = getTextureResources(d3d9Device);
  832. if (textureResources == NULL || textureResources->pBaseTex == NULL)
  833. {
  834. createTextureResources(d3d9Device);
  835. textureResources = getTextureResources(d3d9Device);
  836. }
  837. assert(textureResources != NULL);
  838. assert(idx < mSurfaceList.size());
  839. return mSurfaceList[idx];
  840. }
  841. //---------------------------------------------------------------------
  842. bool D3D9Texture::useDefaultPool()
  843. {
  844. // Determine D3D pool to use
  845. // Use managed unless user has asked for
  846. // a dynamic texture, and device supports D3DUSAGE_DYNAMIC (because default pool
  847. // resources without the dynamic flag are not lockable)
  848. return ((mUsage & TU_DYNAMIC) && mDynamicTextures);
  849. }
  850. //---------------------------------------------------------------------
  851. void D3D9Texture::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  852. {
  853. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  854. if (D3D9RenderSystem::getResourceManager()->getCreationPolicy() == RCP_CREATE_ON_ALL_DEVICES)
  855. createTextureResources(d3d9Device);
  856. }
  857. //---------------------------------------------------------------------
  858. void D3D9Texture::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  859. {
  860. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  861. DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.find(d3d9Device);
  862. if (it != mMapDeviceToTextureResources.end())
  863. {
  864. TextureResources* textureResource = it->second;
  865. // Destroy surfaces from each mip level.
  866. for(unsigned int i = 0; i < mSurfaceList.size(); ++i)
  867. {
  868. D3D9HardwarePixelBuffer* pixelBuffer = static_cast<D3D9HardwarePixelBuffer*>(mSurfaceList[i].get());
  869. pixelBuffer->destroyBufferResources(d3d9Device);
  870. }
  871. // Just free any internal resources, don't call unload() here
  872. // because we want the un-touched resource to keep its unloaded status
  873. // after device reset.
  874. freeTextureResources(d3d9Device, textureResource);
  875. SAFE_DELETE(textureResource);
  876. mMapDeviceToTextureResources.erase(it);
  877. }
  878. }
  879. //---------------------------------------------------------------------
  880. void D3D9Texture::notifyOnDeviceLost(IDirect3DDevice9* d3d9Device)
  881. {
  882. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  883. if(mD3DPool == D3DPOOL_DEFAULT)
  884. {
  885. DeviceToTextureResourcesIterator it = mMapDeviceToTextureResources.find(d3d9Device);
  886. if (it != mMapDeviceToTextureResources.end())
  887. {
  888. TextureResources* textureResource = it->second;
  889. // Just free any internal resources, don't call unload() here
  890. // because we want the un-touched resource to keep its unloaded status
  891. // after device reset.
  892. freeTextureResources(d3d9Device, textureResource);
  893. }
  894. }
  895. }
  896. //---------------------------------------------------------------------
  897. void D3D9Texture::notifyOnDeviceReset(IDirect3DDevice9* d3d9Device)
  898. {
  899. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  900. if(mD3DPool == D3DPOOL_DEFAULT)
  901. {
  902. createTextureResources(d3d9Device);
  903. }
  904. }
  905. //---------------------------------------------------------------------
  906. IDirect3DBaseTexture9* D3D9Texture::getTexture_internal()
  907. {
  908. THROW_IF_NOT_RENDER_THREAD;
  909. TextureResources* textureResources;
  910. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  911. textureResources = getTextureResources(d3d9Device);
  912. if (textureResources == NULL || textureResources->pBaseTex == NULL)
  913. {
  914. createTextureResources(d3d9Device);
  915. textureResources = getTextureResources(d3d9Device);
  916. }
  917. assert(textureResources);
  918. assert(textureResources->pBaseTex);
  919. return textureResources->pBaseTex;
  920. }
  921. //---------------------------------------------------------------------
  922. IDirect3DTexture9* D3D9Texture::getNormTexture_internal()
  923. {
  924. THROW_IF_NOT_RENDER_THREAD;
  925. TextureResources* textureResources;
  926. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  927. textureResources = getTextureResources(d3d9Device);
  928. if (textureResources == NULL || textureResources->pNormTex == NULL)
  929. {
  930. createTextureResources(d3d9Device);
  931. textureResources = getTextureResources(d3d9Device);
  932. }
  933. assert(textureResources);
  934. assert(textureResources->pNormTex);
  935. return textureResources->pNormTex;
  936. }
  937. //---------------------------------------------------------------------
  938. IDirect3DCubeTexture9* D3D9Texture::getCubeTexture_internal()
  939. {
  940. THROW_IF_NOT_RENDER_THREAD;
  941. TextureResources* textureResources;
  942. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  943. textureResources = getTextureResources(d3d9Device);
  944. if (textureResources == NULL || textureResources->pCubeTex)
  945. {
  946. createTextureResources(d3d9Device);
  947. textureResources = getTextureResources(d3d9Device);
  948. }
  949. assert(textureResources);
  950. assert(textureResources->pCubeTex);
  951. return textureResources->pCubeTex;
  952. }
  953. /****************************************************************************************/
  954. D3D9RenderTexture::D3D9RenderTexture(const String &name,
  955. D3D9HardwarePixelBuffer *buffer,
  956. bool writeGamma,
  957. UINT32 fsaa) : RenderTexture(buffer, 0)
  958. {
  959. mName = name;
  960. mHwGamma = writeGamma;
  961. mFSAA = fsaa;
  962. }
  963. //---------------------------------------------------------------------
  964. void D3D9RenderTexture::update(bool swap)
  965. {
  966. D3D9DeviceManager* deviceManager = D3D9RenderSystem::getDeviceManager();
  967. D3D9Device* currRenderWindowDevice = deviceManager->getActiveRenderTargetDevice();
  968. if (currRenderWindowDevice != NULL)
  969. {
  970. if (currRenderWindowDevice->isDeviceLost() == false)
  971. RenderTexture::update(swap);
  972. }
  973. else
  974. {
  975. for (UINT i=0; i < deviceManager->getDeviceCount(); ++i)
  976. {
  977. D3D9Device* device = deviceManager->getDevice(i);
  978. if (device->isDeviceLost() == false)
  979. {
  980. deviceManager->setActiveRenderTargetDevice(device);
  981. RenderTexture::update(swap);
  982. deviceManager->setActiveRenderTargetDevice(NULL);
  983. }
  984. }
  985. }
  986. }
  987. //---------------------------------------------------------------------
  988. void D3D9RenderTexture::getCustomAttribute_internal( const String& name, void *pData )
  989. {
  990. if(name == "DDBACKBUFFER")
  991. {
  992. if (mFSAA > 0)
  993. {
  994. // rendering to AA surface
  995. IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
  996. *pSurf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer)->getFSAASurface(D3D9RenderSystem::getActiveD3D9Device());
  997. return;
  998. }
  999. else
  1000. {
  1001. IDirect3DSurface9 ** pSurf = (IDirect3DSurface9 **)pData;
  1002. *pSurf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer)->getSurface(D3D9RenderSystem::getActiveD3D9Device());
  1003. return;
  1004. }
  1005. }
  1006. else if(name == "HWND")
  1007. {
  1008. HWND *pHwnd = (HWND*)pData;
  1009. *pHwnd = NULL;
  1010. return;
  1011. }
  1012. else if(name == "BUFFER")
  1013. {
  1014. *static_cast<HardwarePixelBuffer**>(pData) = mBuffer;
  1015. return;
  1016. }
  1017. }
  1018. //---------------------------------------------------------------------
  1019. void D3D9RenderTexture::swapBuffers(bool waitForVSync /* = true */)
  1020. {
  1021. // Only needed if we have to blit from AA surface
  1022. if (mFSAA > 0)
  1023. {
  1024. D3D9DeviceManager* deviceManager = D3D9RenderSystem::getDeviceManager();
  1025. D3D9HardwarePixelBuffer* buf = static_cast<D3D9HardwarePixelBuffer*>(mBuffer);
  1026. for (UINT i=0; i < deviceManager->getDeviceCount(); ++i)
  1027. {
  1028. D3D9Device* device = deviceManager->getDevice(i);
  1029. if (device->isDeviceLost() == false)
  1030. {
  1031. IDirect3DDevice9* d3d9Device = device->getD3D9Device();
  1032. HRESULT hr = d3d9Device->StretchRect(buf->getFSAASurface(d3d9Device), 0,
  1033. buf->getSurface(d3d9Device), 0, D3DTEXF_NONE);
  1034. if (FAILED(hr))
  1035. {
  1036. CM_EXCEPT(InternalErrorException, "Unable to copy AA buffer to final buffer: " + String(DXGetErrorDescription(hr)));
  1037. }
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }
  1043. #undef THROW_IF_NOT_RENDER_THREAD