CmD3D9Texture.cpp 39 KB

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