CmD3D9Texture.cpp 40 KB

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