BsD3D9Texture.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. #include "BsCoreThread.h"
  2. #include "BsD3D9Texture.h"
  3. #include "BsD3D9PixelBuffer.h"
  4. #include "BsException.h"
  5. #include "BsBitwise.h"
  6. #include "BsD3D9Mappings.h"
  7. #include "BsD3D9RenderSystem.h"
  8. #include "BsD3D9TextureManager.h"
  9. #include "BsD3D9Device.h"
  10. #include "BsD3D9DeviceManager.h"
  11. #include "BsD3D9ResourceManager.h"
  12. #include "BsRenderStats.h"
  13. namespace BansheeEngine
  14. {
  15. D3D9Texture::D3D9Texture()
  16. :Texture(), mD3DPool(D3DPOOL_MANAGED), mDynamicTextures(false),
  17. mHwGammaReadSupported(false), mHwGammaWriteSupported(false), mMultisampleType(D3DMULTISAMPLE_NONE),
  18. mMultisampleQuality(0), mIsBindableAsShaderResource(true)
  19. { }
  20. D3D9Texture::~D3D9Texture()
  21. {
  22. }
  23. void D3D9Texture::initialize_internal()
  24. {
  25. THROW_IF_NOT_CORE_THREAD;
  26. for (UINT32 i = 0; i < D3D9RenderSystem::getResourceCreationDeviceCount(); ++i)
  27. {
  28. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getResourceCreationDevice(i);
  29. createInternalResources(d3d9Device);
  30. }
  31. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_Texture);
  32. Texture::initialize_internal();
  33. }
  34. void D3D9Texture::destroy_internal()
  35. {
  36. THROW_IF_NOT_CORE_THREAD;
  37. for (auto& resPair : mMapDeviceToTextureResources)
  38. {
  39. TextureResources* textureResource = resPair.second;
  40. freeTextureResources(resPair.first, textureResource);
  41. }
  42. for (auto& resPair : mMapDeviceToTextureResources)
  43. {
  44. TextureResources* textureResource = resPair.second;
  45. if (textureResource != nullptr)
  46. bs_delete(textureResource);
  47. }
  48. mMapDeviceToTextureResources.clear();
  49. mSurfaceList.clear();
  50. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_Texture);
  51. Texture::destroy_internal();
  52. }
  53. PixelData D3D9Texture::lockImpl(GpuLockOptions options, UINT32 mipLevel, UINT32 face)
  54. {
  55. if (mMultisampleCount > 0)
  56. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  57. if(mLockedBuffer != nullptr)
  58. BS_EXCEPT(InternalErrorException, "Trying to lock a buffer that's already locked.");
  59. if(getUsage() == TU_DEPTHSTENCIL)
  60. BS_EXCEPT(InternalErrorException, "Cannot lock a depth stencil texture.");
  61. UINT32 mipWidth = mWidth >> mipLevel;
  62. UINT32 mipHeight = mHeight >> mipLevel;
  63. UINT32 mipDepth = mDepth >> mipLevel;
  64. PixelData lockedArea(mipWidth, mipHeight, mipDepth, mFormat);
  65. mLockedBuffer = getBuffer(face, mipLevel);
  66. lockedArea.setExternalBuffer((UINT8*)mLockedBuffer->lock(options));
  67. return lockedArea;
  68. }
  69. void D3D9Texture::unlockImpl()
  70. {
  71. if(mLockedBuffer == nullptr)
  72. BS_EXCEPT(InternalErrorException, "Trying to unlock a buffer that's not locked.");
  73. mLockedBuffer->unlock();
  74. mLockedBuffer = nullptr;
  75. }
  76. void D3D9Texture::readData(PixelData& dest, UINT32 mipLevel, UINT32 face)
  77. {
  78. if (mMultisampleCount > 0)
  79. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  80. if (mUsage == TU_DEPTHSTENCIL || mUsage == TU_RENDERTARGET) // Render targets cannot be locked normally
  81. {
  82. IDirect3DDevice9* device = D3D9RenderSystem::getActiveD3D9Device();
  83. D3D9PixelBuffer* sourceBuffer = static_cast<D3D9PixelBuffer*>(getBuffer(face, mipLevel).get());
  84. UINT32 mipWidth = mWidth >> mipLevel;
  85. UINT32 mipHeight = mHeight >> mipLevel;
  86. D3DFORMAT format = chooseD3DFormat(device);
  87. // Note: I'm allocating and releasing a texture every time we read.
  88. // I might consider adding a flag to keep this texture permanent which would make reading
  89. // faster but at the cost of double the memory.
  90. IDirect3DTexture9* stagingTexture = nullptr;
  91. HRESULT hr = D3DXCreateTexture(device, (UINT)mipWidth, (UINT)mipHeight, 1,
  92. 0, format, D3DPOOL_SYSTEMMEM, &stagingTexture);
  93. if (FAILED(hr))
  94. {
  95. String msg = DXGetErrorDescription(hr);
  96. BS_EXCEPT(RenderingAPIException, "Failed to create a texture: " + msg);
  97. }
  98. IDirect3DSurface9* stagingSurface = nullptr;
  99. hr = stagingTexture->GetSurfaceLevel(0, &stagingSurface);
  100. if (FAILED(hr))
  101. {
  102. String msg = DXGetErrorDescription(hr);
  103. BS_EXCEPT(RenderingAPIException, "Failed to retrieve a texture surface: " + msg);
  104. }
  105. hr = device->GetRenderTargetData(sourceBuffer->getSurface(device), stagingSurface);
  106. if (FAILED(hr))
  107. {
  108. String msg = DXGetErrorDescription(hr);
  109. BS_EXCEPT(RenderingAPIException, "Failed to retrieve render target data: " + msg);
  110. }
  111. PixelData myData(mipWidth, mipHeight, 1, mFormat);
  112. D3DLOCKED_RECT lrect;
  113. hr = stagingSurface->LockRect(&lrect, nullptr, D3DLOCK_READONLY);
  114. if (FAILED(hr))
  115. {
  116. String msg = DXGetErrorDescription(hr);
  117. BS_EXCEPT(RenderingAPIException, "Failed to lock surface: " + msg);
  118. }
  119. D3D9PixelBuffer::initPixelDataFromD3DLock(myData, lrect);
  120. #if BS_DEBUG_MODE
  121. if (dest.getConsecutiveSize() != myData.getConsecutiveSize())
  122. BS_EXCEPT(InternalErrorException, "Buffer sizes don't match.");
  123. #endif
  124. PixelUtil::bulkPixelConversion(myData, dest);
  125. hr = stagingSurface->UnlockRect();
  126. if (FAILED(hr))
  127. {
  128. String msg = DXGetErrorDescription(hr);
  129. BS_EXCEPT(RenderingAPIException, "Failed to unlock surface: " + msg);
  130. }
  131. SAFE_RELEASE(stagingSurface);
  132. SAFE_RELEASE(stagingTexture);
  133. }
  134. else
  135. {
  136. PixelData myData = lock(GBL_READ_ONLY, mipLevel, face);
  137. #if BS_DEBUG_MODE
  138. if (dest.getConsecutiveSize() != myData.getConsecutiveSize())
  139. {
  140. unlock();
  141. BS_EXCEPT(InternalErrorException, "Buffer sizes don't match.");
  142. }
  143. #endif
  144. PixelUtil::bulkPixelConversion(myData, dest);
  145. unlock();
  146. }
  147. }
  148. void D3D9Texture::writeData(const PixelData& src, UINT32 mipLevel, UINT32 face, bool discardWholeBuffer)
  149. {
  150. if (mMultisampleCount > 0)
  151. BS_EXCEPT(InvalidStateException, "Multisampled textures cannot be accessed from the CPU directly.");
  152. if(mUsage == TU_DYNAMIC || mUsage == TU_STATIC)
  153. {
  154. PixelData myData = lock(discardWholeBuffer ? GBL_WRITE_ONLY_DISCARD : GBL_WRITE_ONLY, mipLevel, face);
  155. PixelUtil::bulkPixelConversion(src, myData);
  156. unlock();
  157. }
  158. else
  159. {
  160. BS_EXCEPT(RenderingAPIException, "Trying to write into a buffer with unsupported usage: " + toString(mUsage));
  161. }
  162. }
  163. void D3D9Texture::copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, TexturePtr& target)
  164. {
  165. THROW_IF_NOT_CORE_THREAD;
  166. if (getTextureType() == TEX_TYPE_1D || getTextureType() == TEX_TYPE_3D)
  167. BS_EXCEPT(NotImplementedException, "Copy not implemented for 1D and 3D textures yet.");
  168. HRESULT hr;
  169. D3D9Texture *other = static_cast<D3D9Texture*>(target.get());
  170. for (auto& resPair : mMapDeviceToTextureResources)
  171. {
  172. TextureResources* srcTextureResource = resPair.second;
  173. TextureResources* dstTextureResource = other->getTextureResources(resPair.first);
  174. IDirect3DSurface9 *sourceSurface = nullptr;
  175. IDirect3DSurface9 *destSurface = nullptr;
  176. if (getTextureType() == TEX_TYPE_2D && srcTextureResource->pNormTex != nullptr && dstTextureResource->pNormTex != nullptr)
  177. {
  178. if(FAILED(hr = srcTextureResource->pNormTex->GetSurfaceLevel(srcMipLevel, &sourceSurface)))
  179. {
  180. String msg = DXGetErrorDescription(hr);
  181. BS_EXCEPT(RenderingAPIException, "Cannot retrieve source surface for copy: " + msg);
  182. }
  183. if(FAILED(hr = dstTextureResource->pNormTex->GetSurfaceLevel(destMipLevel, &destSurface)))
  184. {
  185. String msg = DXGetErrorDescription(hr);
  186. BS_EXCEPT(RenderingAPIException, "Cannot retrieve destination surface for copy: " + msg);
  187. }
  188. }
  189. else if (getTextureType() == TEX_TYPE_CUBE_MAP && srcTextureResource->pCubeTex != nullptr && dstTextureResource->pCubeTex != nullptr)
  190. {
  191. IDirect3DSurface9 *sourceSurface = nullptr;
  192. if (FAILED(hr = srcTextureResource->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)srcFace, srcMipLevel, &sourceSurface)))
  193. {
  194. String msg = DXGetErrorDescription(hr);
  195. BS_EXCEPT(RenderingAPIException, "Cannot retrieve source surface for copy: " + msg);
  196. }
  197. IDirect3DSurface9 *destSurface = nullptr;
  198. if (FAILED(hr = dstTextureResource->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)destFace, destMipLevel, &destSurface)))
  199. {
  200. String msg = DXGetErrorDescription(hr);
  201. BS_EXCEPT(RenderingAPIException, "Cannot retrieve destination surface for copy: " + msg);
  202. }
  203. }
  204. if (sourceSurface != nullptr && destSurface != nullptr)
  205. {
  206. if (sourceSurface->Pool != D3DPOOL_DEFAULT)
  207. BS_EXCEPT(InvalidStateException, "Source surface must be in the default pool.");
  208. if (destSurface->Pool != D3DPOOL_DEFAULT)
  209. BS_EXCEPT(InvalidStateException, "Destination surface must be in the default pool.");
  210. if (FAILED(hr = resPair.first->StretchRect(sourceSurface, NULL, destSurface, NULL, D3DTEXF_NONE)))
  211. {
  212. String msg = DXGetErrorDescription(hr);
  213. BS_EXCEPT(RenderingAPIException, "StretchRect failed during copy: " + msg);
  214. }
  215. }
  216. SAFE_RELEASE(sourceSurface);
  217. SAFE_RELEASE(destSurface);
  218. }
  219. }
  220. D3D9Texture::TextureResources* D3D9Texture::getTextureResources(IDirect3DDevice9* d3d9Device)
  221. {
  222. auto iterFind = mMapDeviceToTextureResources.find(d3d9Device);
  223. if (iterFind == mMapDeviceToTextureResources.end())
  224. return nullptr;
  225. return iterFind->second;
  226. }
  227. D3D9Texture::TextureResources* D3D9Texture::allocateTextureResources(IDirect3DDevice9* d3d9Device)
  228. {
  229. assert(mMapDeviceToTextureResources.find(d3d9Device) == mMapDeviceToTextureResources.end());
  230. TextureResources* textureResources = bs_new<TextureResources>();
  231. textureResources->pNormTex = nullptr;
  232. textureResources->pCubeTex = nullptr;
  233. textureResources->pVolumeTex = nullptr;
  234. textureResources->pBaseTex = nullptr;
  235. textureResources->pMultisampleSurface = nullptr;
  236. textureResources->pDepthStencilSurface = nullptr;
  237. mMapDeviceToTextureResources[d3d9Device] = textureResources;
  238. return textureResources;
  239. }
  240. void D3D9Texture::freeTextureResources(IDirect3DDevice9* d3d9Device, D3D9Texture::TextureResources* textureResources)
  241. {
  242. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  243. // Release surfaces from each mip level.
  244. for(unsigned int i = 0; i < mSurfaceList.size(); ++i)
  245. {
  246. D3D9PixelBuffer* pixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[i].get());
  247. pixelBuffer->releaseSurfaces(d3d9Device);
  248. }
  249. // Release the rest of the resources.
  250. SAFE_RELEASE(textureResources->pBaseTex);
  251. SAFE_RELEASE(textureResources->pNormTex);
  252. SAFE_RELEASE(textureResources->pCubeTex);
  253. SAFE_RELEASE(textureResources->pVolumeTex);
  254. SAFE_RELEASE(textureResources->pMultisampleSurface);
  255. SAFE_RELEASE(textureResources->pDepthStencilSurface);
  256. }
  257. UINT32 D3D9Texture::calculateSize() const
  258. {
  259. UINT32 instanceSize = getNumFaces() * PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat);
  260. return instanceSize * (UINT32)mMapDeviceToTextureResources.size();
  261. }
  262. void D3D9Texture::determinePool()
  263. {
  264. if (useDefaultPool())
  265. mD3DPool = D3DPOOL_DEFAULT;
  266. else
  267. mD3DPool = D3DPOOL_MANAGED;
  268. }
  269. void D3D9Texture::createInternalResources(IDirect3DDevice9* d3d9Device)
  270. {
  271. switch (getTextureType())
  272. {
  273. case TEX_TYPE_1D:
  274. case TEX_TYPE_2D:
  275. createNormTex(d3d9Device);
  276. break;
  277. case TEX_TYPE_CUBE_MAP:
  278. createCubeTex(d3d9Device);
  279. break;
  280. case TEX_TYPE_3D:
  281. createVolumeTex(d3d9Device);
  282. break;
  283. default:
  284. destroy_internal();
  285. BS_EXCEPT(InternalErrorException, "Unknown texture type.");
  286. }
  287. }
  288. void D3D9Texture::createNormTex(IDirect3DDevice9* d3d9Device)
  289. {
  290. assert(mWidth > 0 || mHeight > 0);
  291. D3DFORMAT d3dPF = chooseD3DFormat(d3d9Device);
  292. if(mFormat != D3D9Mappings::_getPF(d3dPF))
  293. {
  294. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  295. }
  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 multisample level
  324. if ((mUsage & TU_RENDERTARGET) != 0 || (mUsage & TU_DEPTHSTENCIL) != 0)
  325. {
  326. D3D9RenderSystem* rsys = static_cast<D3D9RenderSystem*>(BansheeEngine::RenderSystem::instancePtr());
  327. rsys->determineMultisampleSettings(d3d9Device, mMultisampleCount, mMultisampleHint, d3dPF, false, &mMultisampleType, &mMultisampleQuality);
  328. }
  329. else
  330. {
  331. mMultisampleType = D3DMULTISAMPLE_NONE;
  332. mMultisampleQuality = 0;
  333. }
  334. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  335. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  336. // Check if mip maps are supported on hardware
  337. if (numMips > 1 && (!(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPMAP) || (mUsage & TU_RENDERTARGET) != 0 || (mUsage & TU_DEPTHSTENCIL) != 0))
  338. {
  339. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: 0");
  340. }
  341. determinePool();
  342. TextureResources* textureResources;
  343. // Get or create new texture resources structure.
  344. textureResources = getTextureResources(d3d9Device);
  345. if (textureResources != NULL)
  346. freeTextureResources(d3d9Device, textureResources);
  347. else
  348. textureResources = allocateTextureResources(d3d9Device);
  349. if ((mUsage & TU_RENDERTARGET) != 0 && (mMultisampleType != D3DMULTISAMPLE_NONE))
  350. {
  351. // Create AA surface
  352. HRESULT hr = d3d9Device->CreateRenderTarget(mWidth, mHeight, d3dPF,
  353. mMultisampleType, mMultisampleQuality,
  354. FALSE,
  355. &textureResources->pMultisampleSurface, NULL);
  356. if (FAILED(hr))
  357. BS_EXCEPT(RenderingAPIException, "Unable to create AA render target: " + String(DXGetErrorDescription(hr)));
  358. D3DSURFACE_DESC desc;
  359. hr = textureResources->pMultisampleSurface->GetDesc(&desc);
  360. if (FAILED(hr))
  361. {
  362. destroy_internal();
  363. BS_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  364. }
  365. setFinalAttributes(d3d9Device, textureResources, desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
  366. mIsBindableAsShaderResource = false; // Cannot bind AA surfaces
  367. }
  368. else if ((mUsage & TU_DEPTHSTENCIL) != 0 && (mMultisampleType != D3DMULTISAMPLE_NONE))
  369. {
  370. // Create AA depth stencil surface
  371. HRESULT hr = d3d9Device->CreateDepthStencilSurface(mWidth, mHeight, d3dPF,
  372. mMultisampleType, mMultisampleQuality,
  373. FALSE,
  374. &textureResources->pDepthStencilSurface, NULL);
  375. if (FAILED(hr))
  376. BS_EXCEPT(RenderingAPIException, "Unable to create AA depth stencil render target: " + String(DXGetErrorDescription(hr)));
  377. // Update final parameters as they may differ from requested ones
  378. D3DSURFACE_DESC desc;
  379. hr = textureResources->pDepthStencilSurface->GetDesc(&desc);
  380. if (FAILED(hr))
  381. {
  382. destroy_internal();
  383. BS_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  384. }
  385. setFinalAttributes(d3d9Device, textureResources, desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
  386. mIsBindableAsShaderResource = false; // Cannot bind AA depth buffer
  387. }
  388. else
  389. {
  390. // Create normal texture
  391. HRESULT hr = D3DXCreateTexture(d3d9Device, (UINT)mWidth, (UINT)mHeight, numMips,
  392. usage, d3dPF, mD3DPool, &textureResources->pNormTex);
  393. if (FAILED(hr))
  394. {
  395. destroy_internal();
  396. BS_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
  397. }
  398. hr = textureResources->pNormTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
  399. if (FAILED(hr))
  400. {
  401. destroy_internal();
  402. BS_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
  403. }
  404. // Update final parameters as they may differ from requested ones
  405. D3DSURFACE_DESC desc;
  406. hr = textureResources->pNormTex->GetLevelDesc(0, &desc);
  407. if (FAILED(hr))
  408. {
  409. destroy_internal();
  410. BS_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  411. }
  412. setFinalAttributes(d3d9Device, textureResources, desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
  413. }
  414. }
  415. void D3D9Texture::createCubeTex(IDirect3DDevice9* d3d9Device)
  416. {
  417. assert(mWidth > 0 || mHeight > 0);
  418. D3DFORMAT d3dPF = chooseD3DFormat(d3d9Device);
  419. if(mFormat != D3D9Mappings::_getPF(d3dPF))
  420. {
  421. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  422. }
  423. // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
  424. DWORD usage = (mUsage & TU_RENDERTARGET) ? D3DUSAGE_RENDERTARGET : 0;
  425. usage |= (mUsage & TU_DEPTHSTENCIL) ? D3DUSAGE_DEPTHSTENCIL : 0;
  426. UINT numMips = (mNumMipmaps == MIP_UNLIMITED) ? D3DX_DEFAULT : mNumMipmaps + 1;
  427. // Check dynamic textures
  428. if (mUsage & TU_DYNAMIC)
  429. {
  430. if (canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF))
  431. {
  432. usage |= D3DUSAGE_DYNAMIC;
  433. mDynamicTextures = true;
  434. }
  435. else
  436. {
  437. mDynamicTextures = false;
  438. }
  439. }
  440. // Check sRGB support
  441. if (mHwGamma)
  442. {
  443. mHwGammaReadSupported = canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF, false);
  444. if (mUsage & TU_RENDERTARGET)
  445. mHwGammaWriteSupported = canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF, true);
  446. }
  447. // No multisampling on cube textures
  448. mMultisampleType = D3DMULTISAMPLE_NONE;
  449. mMultisampleQuality = 0;
  450. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  451. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  452. // Check if mip map cube textures are supported
  453. if (numMips > 1 && !(deviceCaps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
  454. {
  455. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: 0");
  456. }
  457. determinePool();
  458. TextureResources* textureResources;
  459. // Get or create new texture resources structure.
  460. textureResources = getTextureResources(d3d9Device);
  461. if (textureResources != NULL)
  462. freeTextureResources(d3d9Device, textureResources);
  463. else
  464. textureResources = allocateTextureResources(d3d9Device);
  465. // Create the texture
  466. HRESULT hr = D3DXCreateCubeTexture(d3d9Device, (UINT)mWidth, numMips,
  467. usage, d3dPF, mD3DPool, &textureResources->pCubeTex);
  468. if (FAILED(hr))
  469. {
  470. destroy_internal();
  471. BS_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
  472. }
  473. hr = textureResources->pCubeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
  474. if (FAILED(hr))
  475. {
  476. destroy_internal();
  477. BS_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
  478. }
  479. // Update final parameters as they may differ from requested ones
  480. D3DSURFACE_DESC desc;
  481. hr = textureResources->pCubeTex->GetLevelDesc(0, &desc);
  482. if (FAILED(hr))
  483. {
  484. destroy_internal();
  485. BS_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  486. }
  487. setFinalAttributes(d3d9Device, textureResources, desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
  488. }
  489. void D3D9Texture::createVolumeTex(IDirect3DDevice9* d3d9Device)
  490. {
  491. assert(mWidth > 0 && mHeight > 0 && mDepth>0);
  492. if (mUsage & TU_RENDERTARGET)
  493. BS_EXCEPT(RenderingAPIException, "D3D9 Volume texture can not be created as render target !!");
  494. if (mUsage & TU_DEPTHSTENCIL)
  495. BS_EXCEPT(RenderingAPIException, "D3D9 Volume texture can not be created as a depth stencil target !!");
  496. D3DFORMAT d3dPF = chooseD3DFormat(d3d9Device);
  497. if(mFormat != D3D9Mappings::_getPF(d3dPF))
  498. {
  499. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  500. }
  501. // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
  502. DWORD usage = (mUsage & TU_RENDERTARGET) ? D3DUSAGE_RENDERTARGET : 0;
  503. UINT numMips = (mNumMipmaps == MIP_UNLIMITED) ? D3DX_DEFAULT : mNumMipmaps + 1;
  504. // Check dynamic textures
  505. if (mUsage & TU_DYNAMIC)
  506. {
  507. if (canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF))
  508. {
  509. usage |= D3DUSAGE_DYNAMIC;
  510. mDynamicTextures = true;
  511. }
  512. else
  513. {
  514. mDynamicTextures = false;
  515. }
  516. }
  517. // Check sRGB support
  518. if (mHwGamma)
  519. {
  520. mHwGammaReadSupported = canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF, false);
  521. if (mUsage & TU_RENDERTARGET)
  522. mHwGammaWriteSupported = canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF, true);
  523. }
  524. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  525. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  526. // Check if mip map volume textures are supported
  527. if (numMips > 1 && !(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
  528. {
  529. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: 0");
  530. }
  531. determinePool();
  532. TextureResources* textureResources;
  533. // Get or create new texture resources structure.
  534. textureResources = getTextureResources(d3d9Device);
  535. if (textureResources != NULL)
  536. freeTextureResources(d3d9Device, textureResources);
  537. else
  538. textureResources = allocateTextureResources(d3d9Device);
  539. // Create the texture
  540. HRESULT hr = D3DXCreateVolumeTexture(d3d9Device, (UINT)mWidth, (UINT)mHeight, (UINT)mDepth,
  541. numMips, usage, d3dPF, mD3DPool, &textureResources->pVolumeTex);
  542. if (FAILED(hr))
  543. {
  544. destroy_internal();
  545. BS_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
  546. }
  547. hr = textureResources->pVolumeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void**)&textureResources->pBaseTex);
  548. if (FAILED(hr))
  549. {
  550. destroy_internal();
  551. BS_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
  552. }
  553. // Update final parameters as they may differ from requested ones
  554. D3DVOLUME_DESC desc;
  555. hr = textureResources->pVolumeTex->GetLevelDesc(0, &desc);
  556. if (FAILED(hr))
  557. {
  558. destroy_internal();
  559. BS_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  560. }
  561. setFinalAttributes(d3d9Device, textureResources, desc.Width, desc.Height, desc.Depth, D3D9Mappings::_getPF(desc.Format));
  562. }
  563. void D3D9Texture::setFinalAttributes(IDirect3DDevice9* d3d9Device, TextureResources* textureResources,
  564. UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  565. {
  566. if(width != mWidth || height != mHeight || depth != mDepth)
  567. {
  568. BS_EXCEPT(InternalErrorException, "Wanted and created textures sizes don't match!" \
  569. "Width: " + toString(width) + "/" + toString(mWidth) +
  570. "Height: " + toString(height) + "/" + toString(mHeight) +
  571. "Depth: " + toString(depth) + "/" + toString(mDepth));
  572. }
  573. if(format != mFormat)
  574. {
  575. BS_EXCEPT(InternalErrorException, "Wanted and created texture formats don't match! " + toString(format) + "/" + toString(mFormat));
  576. }
  577. createSurfaceList(d3d9Device, textureResources);
  578. }
  579. D3DTEXTUREFILTERTYPE D3D9Texture::getBestFilterMethod(IDirect3DDevice9* d3d9Device)
  580. {
  581. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  582. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  583. DWORD filterCaps = 0;
  584. switch (getTextureType())
  585. {
  586. case TEX_TYPE_1D:
  587. // Same as 2D
  588. case TEX_TYPE_2D:
  589. filterCaps = deviceCaps.TextureFilterCaps;
  590. break;
  591. case TEX_TYPE_3D:
  592. filterCaps = deviceCaps.VolumeTextureFilterCaps;
  593. break;
  594. case TEX_TYPE_CUBE_MAP:
  595. filterCaps = deviceCaps.CubeTextureFilterCaps;
  596. break;
  597. }
  598. if(filterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
  599. return D3DTEXF_GAUSSIANQUAD;
  600. if(filterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
  601. return D3DTEXF_PYRAMIDALQUAD;
  602. if(filterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
  603. return D3DTEXF_ANISOTROPIC;
  604. if(filterCaps & D3DPTFILTERCAPS_MINFLINEAR)
  605. return D3DTEXF_LINEAR;
  606. if(filterCaps & D3DPTFILTERCAPS_MINFPOINT)
  607. return D3DTEXF_POINT;
  608. return D3DTEXF_POINT;
  609. }
  610. bool D3D9Texture::canUseDynamicTextures(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType,
  611. D3DFORMAT srcFormat)
  612. {
  613. IDirect3D9* pD3D = nullptr;
  614. HRESULT hr = d3d9Device->GetDirect3D(&pD3D);
  615. if (FAILED(hr))
  616. BS_EXCEPT(InvalidParametersException, "GetDirect3D failed !!!");
  617. if (pD3D != nullptr)
  618. pD3D->Release();
  619. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  620. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  621. D3DFORMAT backBufferFormat = device->getBackBufferFormat();
  622. hr = pD3D->CheckDeviceFormat(deviceCaps.AdapterOrdinal, deviceCaps.DeviceType,
  623. backBufferFormat, srcUsage | D3DUSAGE_DYNAMIC, srcType, srcFormat);
  624. return hr == D3D_OK;
  625. }
  626. bool D3D9Texture::canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device, DWORD srcUsage,
  627. D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting)
  628. {
  629. IDirect3D9* pD3D = nullptr;
  630. HRESULT hr = d3d9Device->GetDirect3D(&pD3D);
  631. if (FAILED(hr))
  632. {
  633. BS_EXCEPT(InvalidParametersException, "GetDirect3D failed !!!" );
  634. }
  635. if (pD3D != nullptr)
  636. pD3D->Release();
  637. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  638. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  639. D3DFORMAT backBufferFormat = device->getBackBufferFormat();
  640. if (forwriting)
  641. srcUsage |= D3DUSAGE_QUERY_SRGBWRITE;
  642. else
  643. srcUsage |= D3DUSAGE_QUERY_SRGBREAD;
  644. hr = pD3D->CheckDeviceFormat(deviceCaps.AdapterOrdinal, deviceCaps.DeviceType,
  645. backBufferFormat, srcUsage, srcType, srcFormat);
  646. return hr == D3D_OK;
  647. }
  648. bool D3D9Texture::canAutoGenMipmaps(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat)
  649. {
  650. IDirect3D9* pD3D = nullptr;
  651. HRESULT hr = d3d9Device->GetDirect3D(&pD3D);
  652. if (FAILED(hr))
  653. {
  654. BS_EXCEPT(InvalidParametersException, "GetDirect3D failed.");
  655. }
  656. if (pD3D != nullptr)
  657. pD3D->Release();
  658. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  659. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  660. D3DFORMAT backBufferFormat = device->getBackBufferFormat();
  661. if (deviceCaps.Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
  662. {
  663. hr = pD3D->CheckDeviceFormat(deviceCaps.AdapterOrdinal, deviceCaps.DeviceType,
  664. backBufferFormat, srcUsage | D3DUSAGE_AUTOGENMIPMAP, srcType, srcFormat);
  665. return hr == D3D_OK;
  666. }
  667. else
  668. return false;
  669. }
  670. D3DFORMAT D3D9Texture::chooseD3DFormat(IDirect3DDevice9* d3d9Device)
  671. {
  672. // Choose frame buffer pixel format in case PF_UNKNOWN was requested
  673. if(mFormat == PF_UNKNOWN)
  674. {
  675. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  676. if((mUsage & TU_DEPTHSTENCIL) != 0)
  677. return device->getDepthStencilFormat();
  678. else
  679. return device->getBackBufferFormat();
  680. }
  681. // Choose closest supported D3D format as a D3D format
  682. return D3D9Mappings::_getPF(D3D9Mappings::_getClosestSupportedPF(mFormat));
  683. }
  684. void D3D9Texture::createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources)
  685. {
  686. assert(textureResources != nullptr);
  687. // Need to know static / dynamic
  688. UINT32 usage;
  689. if ((mUsage & TU_DYNAMIC) && mDynamicTextures)
  690. {
  691. usage = GBU_DYNAMIC;
  692. }
  693. else
  694. {
  695. usage = GBU_STATIC;
  696. }
  697. if (mUsage & TU_RENDERTARGET)
  698. {
  699. usage |= TU_RENDERTARGET;
  700. }
  701. else if(mUsage & TU_DEPTHSTENCIL)
  702. {
  703. usage |= TU_RENDERTARGET;
  704. }
  705. UINT32 surfaceCount = static_cast<UINT32>((getNumFaces() * (mNumMipmaps + 1)));
  706. bool updateOldList = mSurfaceList.size() == surfaceCount;
  707. if(!updateOldList)
  708. {
  709. mSurfaceList.clear();
  710. for(UINT32 face = 0; face < getNumFaces(); face++)
  711. {
  712. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  713. {
  714. mSurfaceList.push_back(bs_shared_ptr<D3D9PixelBuffer>((GpuBufferUsage)usage, this));
  715. }
  716. }
  717. }
  718. IDirect3DSurface9* surface = nullptr;
  719. IDirect3DVolume9* volume = nullptr;
  720. if((mUsage & TU_RENDERTARGET) != 0 && (mMultisampleType != D3DMULTISAMPLE_NONE))
  721. {
  722. assert(textureResources->pMultisampleSurface);
  723. assert(getTextureType() == TEX_TYPE_2D);
  724. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[0].get());
  725. currPixelBuffer->bind(d3d9Device, textureResources->pMultisampleSurface, textureResources->pBaseTex);
  726. }
  727. else if((mUsage & TU_DEPTHSTENCIL) != 0 && (mMultisampleType != D3DMULTISAMPLE_NONE))
  728. {
  729. assert(textureResources->pDepthStencilSurface);
  730. assert(getTextureType() == TEX_TYPE_2D);
  731. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[0].get());
  732. currPixelBuffer->bind(d3d9Device, textureResources->pDepthStencilSurface, textureResources->pBaseTex);
  733. }
  734. else
  735. {
  736. assert(textureResources->pBaseTex);
  737. UINT32 numCreatedMips = textureResources->pBaseTex->GetLevelCount() - 1;
  738. if(numCreatedMips != mNumMipmaps)
  739. {
  740. BS_EXCEPT(InternalErrorException, "Number of created and wanted mip map levels doesn't match: " +
  741. toString(numCreatedMips) + "/" + toString(mNumMipmaps));
  742. }
  743. switch(getTextureType())
  744. {
  745. case TEX_TYPE_2D:
  746. case TEX_TYPE_1D:
  747. assert(textureResources->pNormTex);
  748. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  749. {
  750. if(textureResources->pNormTex->GetSurfaceLevel(static_cast<UINT>(mip), &surface) != D3D_OK)
  751. BS_EXCEPT(RenderingAPIException, "Get surface level failed");
  752. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[mip].get());
  753. currPixelBuffer->bind(d3d9Device, surface, textureResources->pBaseTex);
  754. surface->Release();
  755. }
  756. break;
  757. case TEX_TYPE_CUBE_MAP:
  758. assert(textureResources->pCubeTex);
  759. for(UINT32 face = 0; face < 6; face++)
  760. {
  761. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  762. {
  763. if(textureResources->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, static_cast<UINT>(mip), &surface) != D3D_OK)
  764. BS_EXCEPT(RenderingAPIException, "Get cubemap surface failed");
  765. UINT32 idx = face*(mNumMipmaps + 1) + mip;
  766. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[idx].get());
  767. currPixelBuffer->bind(d3d9Device, surface, textureResources->pBaseTex);
  768. surface->Release();
  769. }
  770. }
  771. break;
  772. case TEX_TYPE_3D:
  773. assert(textureResources->pVolumeTex);
  774. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  775. {
  776. if(textureResources->pVolumeTex->GetVolumeLevel(static_cast<UINT>(mip), &volume) != D3D_OK)
  777. BS_EXCEPT(RenderingAPIException, "Get volume level failed");
  778. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[mip].get());
  779. currPixelBuffer->bind(d3d9Device, volume, textureResources->pBaseTex);
  780. volume->Release();
  781. }
  782. break;
  783. };
  784. }
  785. }
  786. PixelBufferPtr D3D9Texture::getBuffer(UINT32 face, UINT32 mipmap)
  787. {
  788. THROW_IF_NOT_CORE_THREAD;
  789. if(face >= getNumFaces())
  790. BS_EXCEPT(InvalidParametersException, "A three dimensional cube has six faces");
  791. if(mipmap > mNumMipmaps)
  792. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  793. UINT32 idx = face*(mNumMipmaps+1) + mipmap;
  794. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  795. TextureResources* textureResources = getTextureResources(d3d9Device);
  796. if (textureResources == nullptr || textureResources->pBaseTex == nullptr)
  797. {
  798. createInternalResources(d3d9Device);
  799. textureResources = getTextureResources(d3d9Device);
  800. }
  801. assert(textureResources != nullptr);
  802. assert(idx < mSurfaceList.size());
  803. return mSurfaceList[idx];
  804. }
  805. bool D3D9Texture::useDefaultPool()
  806. {
  807. return (mUsage & TU_RENDERTARGET) || (mUsage & TU_DEPTHSTENCIL) || ((mUsage & TU_DYNAMIC) && mDynamicTextures);
  808. }
  809. void D3D9Texture::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  810. {
  811. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  812. if (D3D9RenderSystem::getResourceManager()->getCreationPolicy() == RCP_CREATE_ON_ALL_DEVICES)
  813. createInternalResources(d3d9Device);
  814. }
  815. void D3D9Texture::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  816. {
  817. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  818. auto iterFind = mMapDeviceToTextureResources.find(d3d9Device);
  819. if (iterFind != mMapDeviceToTextureResources.end())
  820. {
  821. TextureResources* textureResource = iterFind->second;
  822. for(unsigned int i = 0; i < mSurfaceList.size(); ++i)
  823. {
  824. D3D9PixelBuffer* pixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[i].get());
  825. pixelBuffer->destroyBufferResources(d3d9Device);
  826. }
  827. freeTextureResources(d3d9Device, textureResource);
  828. if(textureResource != nullptr)
  829. bs_delete(textureResource);
  830. mMapDeviceToTextureResources.erase(iterFind);
  831. }
  832. }
  833. void D3D9Texture::notifyOnDeviceLost(IDirect3DDevice9* d3d9Device)
  834. {
  835. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  836. if(mD3DPool == D3DPOOL_DEFAULT)
  837. {
  838. auto iterFind = mMapDeviceToTextureResources.find(d3d9Device);
  839. if (iterFind != mMapDeviceToTextureResources.end())
  840. {
  841. TextureResources* textureResource = iterFind->second;
  842. freeTextureResources(d3d9Device, textureResource);
  843. }
  844. }
  845. }
  846. void D3D9Texture::notifyOnDeviceReset(IDirect3DDevice9* d3d9Device)
  847. {
  848. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  849. if(mD3DPool == D3DPOOL_DEFAULT)
  850. {
  851. createInternalResources(d3d9Device);
  852. }
  853. }
  854. IDirect3DBaseTexture9* D3D9Texture::getTexture_internal()
  855. {
  856. THROW_IF_NOT_CORE_THREAD;
  857. TextureResources* textureResources;
  858. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  859. textureResources = getTextureResources(d3d9Device);
  860. if (textureResources == nullptr || textureResources->pBaseTex == nullptr)
  861. {
  862. createInternalResources(d3d9Device);
  863. textureResources = getTextureResources(d3d9Device);
  864. }
  865. assert(textureResources);
  866. assert(textureResources->pBaseTex);
  867. return textureResources->pBaseTex;
  868. }
  869. IDirect3DTexture9* D3D9Texture::getNormTexture_internal()
  870. {
  871. THROW_IF_NOT_CORE_THREAD;
  872. TextureResources* textureResources;
  873. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  874. textureResources = getTextureResources(d3d9Device);
  875. if (textureResources == nullptr || textureResources->pNormTex == nullptr)
  876. {
  877. createInternalResources(d3d9Device);
  878. textureResources = getTextureResources(d3d9Device);
  879. }
  880. assert(textureResources);
  881. assert(textureResources->pNormTex);
  882. return textureResources->pNormTex;
  883. }
  884. IDirect3DCubeTexture9* D3D9Texture::getCubeTexture_internal()
  885. {
  886. THROW_IF_NOT_CORE_THREAD;
  887. TextureResources* textureResources;
  888. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  889. textureResources = getTextureResources(d3d9Device);
  890. if (textureResources == nullptr || textureResources->pCubeTex)
  891. {
  892. createInternalResources(d3d9Device);
  893. textureResources = getTextureResources(d3d9Device);
  894. }
  895. assert(textureResources);
  896. assert(textureResources->pCubeTex);
  897. return textureResources->pCubeTex;
  898. }
  899. }