CmD3D9Texture.cpp 41 KB

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