CmD3D9Texture.cpp 42 KB

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