BsD3D9Texture.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. if (mUsage & TU_RENDERTARGET)
  419. BS_EXCEPT(RenderingAPIException, "D3D9 Cube texture can not be created as render target !!");
  420. if (mUsage & TU_DEPTHSTENCIL)
  421. BS_EXCEPT(RenderingAPIException, "D3D9 Cube texture can not be created as a depth stencil target !!");
  422. D3DFORMAT d3dPF = chooseD3DFormat(d3d9Device);
  423. if(mFormat != D3D9Mappings::_getPF(d3dPF))
  424. {
  425. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  426. }
  427. // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
  428. DWORD usage = (mUsage & TU_RENDERTARGET) ? D3DUSAGE_RENDERTARGET : 0;
  429. UINT numMips = (mNumMipmaps == MIP_UNLIMITED) ? D3DX_DEFAULT : mNumMipmaps + 1;
  430. // Check dynamic textures
  431. if (mUsage & TU_DYNAMIC)
  432. {
  433. if (canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF))
  434. {
  435. usage |= D3DUSAGE_DYNAMIC;
  436. mDynamicTextures = true;
  437. }
  438. else
  439. {
  440. mDynamicTextures = false;
  441. }
  442. }
  443. // Check sRGB support
  444. if (mHwGamma)
  445. {
  446. mHwGammaReadSupported = canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF, false);
  447. if (mUsage & TU_RENDERTARGET)
  448. mHwGammaWriteSupported = canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_CUBETEXTURE, d3dPF, true);
  449. }
  450. // No multisampling on cube textures
  451. mMultisampleType = D3DMULTISAMPLE_NONE;
  452. mMultisampleQuality = 0;
  453. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  454. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  455. // Check if mip map cube textures are supported
  456. if (numMips > 1 && !(deviceCaps.TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP))
  457. {
  458. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: 0");
  459. }
  460. determinePool();
  461. TextureResources* textureResources;
  462. // Get or create new texture resources structure.
  463. textureResources = getTextureResources(d3d9Device);
  464. if (textureResources != NULL)
  465. freeTextureResources(d3d9Device, textureResources);
  466. else
  467. textureResources = allocateTextureResources(d3d9Device);
  468. // Create the texture
  469. HRESULT hr = D3DXCreateCubeTexture(d3d9Device, (UINT)mWidth, numMips,
  470. usage, d3dPF, mD3DPool, &textureResources->pCubeTex);
  471. if (FAILED(hr))
  472. {
  473. destroy_internal();
  474. BS_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
  475. }
  476. hr = textureResources->pCubeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void **)&textureResources->pBaseTex);
  477. if (FAILED(hr))
  478. {
  479. destroy_internal();
  480. BS_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
  481. }
  482. // Update final parameters as they may differ from requested ones
  483. D3DSURFACE_DESC desc;
  484. hr = textureResources->pCubeTex->GetLevelDesc(0, &desc);
  485. if (FAILED(hr))
  486. {
  487. destroy_internal();
  488. BS_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  489. }
  490. setFinalAttributes(d3d9Device, textureResources, desc.Width, desc.Height, 1, D3D9Mappings::_getPF(desc.Format));
  491. }
  492. void D3D9Texture::createVolumeTex(IDirect3DDevice9* d3d9Device)
  493. {
  494. assert(mWidth > 0 && mHeight > 0 && mDepth>0);
  495. if (mUsage & TU_RENDERTARGET)
  496. BS_EXCEPT(RenderingAPIException, "D3D9 Volume texture can not be created as render target !!");
  497. if (mUsage & TU_DEPTHSTENCIL)
  498. BS_EXCEPT(RenderingAPIException, "D3D9 Volume texture can not be created as a depth stencil target !!");
  499. D3DFORMAT d3dPF = chooseD3DFormat(d3d9Device);
  500. if(mFormat != D3D9Mappings::_getPF(d3dPF))
  501. {
  502. BS_EXCEPT(RenderingAPIException, "Provided pixel format is not supported by the driver: " + toString(mFormat));
  503. }
  504. // Use D3DX to help us create the texture, this way it can adjust any relevant sizes
  505. DWORD usage = (mUsage & TU_RENDERTARGET) ? D3DUSAGE_RENDERTARGET : 0;
  506. UINT numMips = (mNumMipmaps == MIP_UNLIMITED) ? D3DX_DEFAULT : mNumMipmaps + 1;
  507. // Check dynamic textures
  508. if (mUsage & TU_DYNAMIC)
  509. {
  510. if (canUseDynamicTextures(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF))
  511. {
  512. usage |= D3DUSAGE_DYNAMIC;
  513. mDynamicTextures = true;
  514. }
  515. else
  516. {
  517. mDynamicTextures = false;
  518. }
  519. }
  520. // Check sRGB support
  521. if (mHwGamma)
  522. {
  523. mHwGammaReadSupported = canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF, false);
  524. if (mUsage & TU_RENDERTARGET)
  525. mHwGammaWriteSupported = canUseHardwareGammaCorrection(d3d9Device, usage, D3DRTYPE_VOLUMETEXTURE, d3dPF, true);
  526. }
  527. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  528. const D3DCAPS9& rkCurCaps = device->getD3D9DeviceCaps();
  529. // Check if mip map volume textures are supported
  530. if (numMips > 1 && !(rkCurCaps.TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP))
  531. {
  532. BS_EXCEPT(InvalidParametersException, "Invalid number of mipmaps. Maximum allowed is: 0");
  533. }
  534. determinePool();
  535. TextureResources* textureResources;
  536. // Get or create new texture resources structure.
  537. textureResources = getTextureResources(d3d9Device);
  538. if (textureResources != NULL)
  539. freeTextureResources(d3d9Device, textureResources);
  540. else
  541. textureResources = allocateTextureResources(d3d9Device);
  542. // Create the texture
  543. HRESULT hr = D3DXCreateVolumeTexture(d3d9Device, (UINT)mWidth, (UINT)mHeight, (UINT)mDepth,
  544. numMips, usage, d3dPF, mD3DPool, &textureResources->pVolumeTex);
  545. if (FAILED(hr))
  546. {
  547. destroy_internal();
  548. BS_EXCEPT(RenderingAPIException, "Error creating texture: " + String(DXGetErrorDescription(hr)));
  549. }
  550. hr = textureResources->pVolumeTex->QueryInterface(IID_IDirect3DBaseTexture9, (void**)&textureResources->pBaseTex);
  551. if (FAILED(hr))
  552. {
  553. destroy_internal();
  554. BS_EXCEPT(RenderingAPIException, "Can't get base texture: " + String(DXGetErrorDescription(hr)));
  555. }
  556. // Update final parameters as they may differ from requested ones
  557. D3DVOLUME_DESC desc;
  558. hr = textureResources->pVolumeTex->GetLevelDesc(0, &desc);
  559. if (FAILED(hr))
  560. {
  561. destroy_internal();
  562. BS_EXCEPT(RenderingAPIException, "Can't get texture description: " + String(DXGetErrorDescription(hr)));
  563. }
  564. setFinalAttributes(d3d9Device, textureResources, desc.Width, desc.Height, desc.Depth, D3D9Mappings::_getPF(desc.Format));
  565. }
  566. void D3D9Texture::setFinalAttributes(IDirect3DDevice9* d3d9Device, TextureResources* textureResources,
  567. UINT32 width, UINT32 height, UINT32 depth, PixelFormat format)
  568. {
  569. if(width != mWidth || height != mHeight || depth != mDepth)
  570. {
  571. BS_EXCEPT(InternalErrorException, "Wanted and created textures sizes don't match!" \
  572. "Width: " + toString(width) + "/" + toString(mWidth) +
  573. "Height: " + toString(height) + "/" + toString(mHeight) +
  574. "Depth: " + toString(depth) + "/" + toString(mDepth));
  575. }
  576. if(format != mFormat)
  577. {
  578. BS_EXCEPT(InternalErrorException, "Wanted and created texture formats don't match! " + toString(format) + "/" + toString(mFormat));
  579. }
  580. createSurfaceList(d3d9Device, textureResources);
  581. }
  582. D3DTEXTUREFILTERTYPE D3D9Texture::getBestFilterMethod(IDirect3DDevice9* d3d9Device)
  583. {
  584. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  585. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  586. DWORD filterCaps = 0;
  587. switch (getTextureType())
  588. {
  589. case TEX_TYPE_1D:
  590. // Same as 2D
  591. case TEX_TYPE_2D:
  592. filterCaps = deviceCaps.TextureFilterCaps;
  593. break;
  594. case TEX_TYPE_3D:
  595. filterCaps = deviceCaps.VolumeTextureFilterCaps;
  596. break;
  597. case TEX_TYPE_CUBE_MAP:
  598. filterCaps = deviceCaps.CubeTextureFilterCaps;
  599. break;
  600. }
  601. if(filterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
  602. return D3DTEXF_GAUSSIANQUAD;
  603. if(filterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
  604. return D3DTEXF_PYRAMIDALQUAD;
  605. if(filterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
  606. return D3DTEXF_ANISOTROPIC;
  607. if(filterCaps & D3DPTFILTERCAPS_MINFLINEAR)
  608. return D3DTEXF_LINEAR;
  609. if(filterCaps & D3DPTFILTERCAPS_MINFPOINT)
  610. return D3DTEXF_POINT;
  611. return D3DTEXF_POINT;
  612. }
  613. bool D3D9Texture::canUseDynamicTextures(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType,
  614. D3DFORMAT srcFormat)
  615. {
  616. IDirect3D9* pD3D = nullptr;
  617. HRESULT hr = d3d9Device->GetDirect3D(&pD3D);
  618. if (FAILED(hr))
  619. BS_EXCEPT(InvalidParametersException, "GetDirect3D failed !!!");
  620. if (pD3D != nullptr)
  621. pD3D->Release();
  622. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  623. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  624. D3DFORMAT backBufferFormat = device->getBackBufferFormat();
  625. hr = pD3D->CheckDeviceFormat(deviceCaps.AdapterOrdinal, deviceCaps.DeviceType,
  626. backBufferFormat, srcUsage | D3DUSAGE_DYNAMIC, srcType, srcFormat);
  627. return hr == D3D_OK;
  628. }
  629. bool D3D9Texture::canUseHardwareGammaCorrection(IDirect3DDevice9* d3d9Device, DWORD srcUsage,
  630. D3DRESOURCETYPE srcType, D3DFORMAT srcFormat, bool forwriting)
  631. {
  632. IDirect3D9* pD3D = nullptr;
  633. HRESULT hr = d3d9Device->GetDirect3D(&pD3D);
  634. if (FAILED(hr))
  635. {
  636. BS_EXCEPT(InvalidParametersException, "GetDirect3D failed !!!" );
  637. }
  638. if (pD3D != nullptr)
  639. pD3D->Release();
  640. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  641. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  642. D3DFORMAT backBufferFormat = device->getBackBufferFormat();
  643. if (forwriting)
  644. srcUsage |= D3DUSAGE_QUERY_SRGBWRITE;
  645. else
  646. srcUsage |= D3DUSAGE_QUERY_SRGBREAD;
  647. hr = pD3D->CheckDeviceFormat(deviceCaps.AdapterOrdinal, deviceCaps.DeviceType,
  648. backBufferFormat, srcUsage, srcType, srcFormat);
  649. return hr == D3D_OK;
  650. }
  651. bool D3D9Texture::canAutoGenMipmaps(IDirect3DDevice9* d3d9Device, DWORD srcUsage, D3DRESOURCETYPE srcType, D3DFORMAT srcFormat)
  652. {
  653. IDirect3D9* pD3D = nullptr;
  654. HRESULT hr = d3d9Device->GetDirect3D(&pD3D);
  655. if (FAILED(hr))
  656. {
  657. BS_EXCEPT(InvalidParametersException, "GetDirect3D failed.");
  658. }
  659. if (pD3D != nullptr)
  660. pD3D->Release();
  661. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  662. const D3DCAPS9& deviceCaps = device->getD3D9DeviceCaps();
  663. D3DFORMAT backBufferFormat = device->getBackBufferFormat();
  664. if (deviceCaps.Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
  665. {
  666. hr = pD3D->CheckDeviceFormat(deviceCaps.AdapterOrdinal, deviceCaps.DeviceType,
  667. backBufferFormat, srcUsage | D3DUSAGE_AUTOGENMIPMAP, srcType, srcFormat);
  668. return hr == D3D_OK;
  669. }
  670. else
  671. return false;
  672. }
  673. D3DFORMAT D3D9Texture::chooseD3DFormat(IDirect3DDevice9* d3d9Device)
  674. {
  675. // Choose frame buffer pixel format in case PF_UNKNOWN was requested
  676. if(mFormat == PF_UNKNOWN)
  677. {
  678. D3D9Device* device = D3D9RenderSystem::getDeviceManager()->getDeviceFromD3D9Device(d3d9Device);
  679. if((mUsage & TU_DEPTHSTENCIL) != 0)
  680. return device->getDepthStencilFormat();
  681. else
  682. return device->getBackBufferFormat();
  683. }
  684. // Choose closest supported D3D format as a D3D format
  685. return D3D9Mappings::_getPF(D3D9Mappings::_getClosestSupportedPF(mFormat));
  686. }
  687. void D3D9Texture::createSurfaceList(IDirect3DDevice9* d3d9Device, TextureResources* textureResources)
  688. {
  689. assert(textureResources != nullptr);
  690. // Need to know static / dynamic
  691. UINT32 usage;
  692. if ((mUsage & TU_DYNAMIC) && mDynamicTextures)
  693. {
  694. usage = GBU_DYNAMIC;
  695. }
  696. else
  697. {
  698. usage = GBU_STATIC;
  699. }
  700. if (mUsage & TU_RENDERTARGET)
  701. {
  702. usage |= TU_RENDERTARGET;
  703. }
  704. else if(mUsage & TU_DEPTHSTENCIL)
  705. {
  706. usage |= TU_RENDERTARGET;
  707. }
  708. UINT32 surfaceCount = static_cast<UINT32>((getNumFaces() * (mNumMipmaps + 1)));
  709. bool updateOldList = mSurfaceList.size() == surfaceCount;
  710. if(!updateOldList)
  711. {
  712. mSurfaceList.clear();
  713. for(UINT32 face = 0; face < getNumFaces(); face++)
  714. {
  715. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  716. {
  717. mSurfaceList.push_back(bs_shared_ptr<D3D9PixelBuffer>((GpuBufferUsage)usage, this));
  718. }
  719. }
  720. }
  721. IDirect3DSurface9* surface = nullptr;
  722. IDirect3DVolume9* volume = nullptr;
  723. if((mUsage & TU_RENDERTARGET) != 0 && (mMultisampleType != D3DMULTISAMPLE_NONE))
  724. {
  725. assert(textureResources->pMultisampleSurface);
  726. assert(getTextureType() == TEX_TYPE_2D);
  727. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[0].get());
  728. currPixelBuffer->bind(d3d9Device, textureResources->pMultisampleSurface, textureResources->pBaseTex);
  729. }
  730. else if((mUsage & TU_DEPTHSTENCIL) != 0 && (mMultisampleType != D3DMULTISAMPLE_NONE))
  731. {
  732. assert(textureResources->pDepthStencilSurface);
  733. assert(getTextureType() == TEX_TYPE_2D);
  734. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[0].get());
  735. currPixelBuffer->bind(d3d9Device, textureResources->pDepthStencilSurface, textureResources->pBaseTex);
  736. }
  737. else
  738. {
  739. assert(textureResources->pBaseTex);
  740. UINT32 numCreatedMips = textureResources->pBaseTex->GetLevelCount() - 1;
  741. if(numCreatedMips != mNumMipmaps)
  742. {
  743. BS_EXCEPT(InternalErrorException, "Number of created and wanted mip map levels doesn't match: " +
  744. toString(numCreatedMips) + "/" + toString(mNumMipmaps));
  745. }
  746. switch(getTextureType())
  747. {
  748. case TEX_TYPE_2D:
  749. case TEX_TYPE_1D:
  750. assert(textureResources->pNormTex);
  751. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  752. {
  753. if(textureResources->pNormTex->GetSurfaceLevel(static_cast<UINT>(mip), &surface) != D3D_OK)
  754. BS_EXCEPT(RenderingAPIException, "Get surface level failed");
  755. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[mip].get());
  756. currPixelBuffer->bind(d3d9Device, surface, textureResources->pBaseTex);
  757. surface->Release();
  758. }
  759. break;
  760. case TEX_TYPE_CUBE_MAP:
  761. assert(textureResources->pCubeTex);
  762. for(UINT32 face = 0; face < 6; face++)
  763. {
  764. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  765. {
  766. if(textureResources->pCubeTex->GetCubeMapSurface((D3DCUBEMAP_FACES)face, static_cast<UINT>(mip), &surface) != D3D_OK)
  767. BS_EXCEPT(RenderingAPIException, "Get cubemap surface failed");
  768. UINT32 idx = face*(mNumMipmaps + 1) + mip;
  769. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[idx].get());
  770. currPixelBuffer->bind(d3d9Device, surface, textureResources->pBaseTex);
  771. surface->Release();
  772. }
  773. }
  774. break;
  775. case TEX_TYPE_3D:
  776. assert(textureResources->pVolumeTex);
  777. for(UINT32 mip = 0; mip <= mNumMipmaps; mip++)
  778. {
  779. if(textureResources->pVolumeTex->GetVolumeLevel(static_cast<UINT>(mip), &volume) != D3D_OK)
  780. BS_EXCEPT(RenderingAPIException, "Get volume level failed");
  781. D3D9PixelBuffer* currPixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[mip].get());
  782. currPixelBuffer->bind(d3d9Device, volume, textureResources->pBaseTex);
  783. volume->Release();
  784. }
  785. break;
  786. };
  787. }
  788. }
  789. PixelBufferPtr D3D9Texture::getBuffer(UINT32 face, UINT32 mipmap)
  790. {
  791. THROW_IF_NOT_CORE_THREAD;
  792. if(face >= getNumFaces())
  793. BS_EXCEPT(InvalidParametersException, "A three dimensional cube has six faces");
  794. if(mipmap > mNumMipmaps)
  795. BS_EXCEPT(InvalidParametersException, "Mipmap index out of range");
  796. UINT32 idx = face*(mNumMipmaps+1) + mipmap;
  797. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  798. TextureResources* textureResources = getTextureResources(d3d9Device);
  799. if (textureResources == nullptr || textureResources->pBaseTex == nullptr)
  800. {
  801. createInternalResources(d3d9Device);
  802. textureResources = getTextureResources(d3d9Device);
  803. }
  804. assert(textureResources != nullptr);
  805. assert(idx < mSurfaceList.size());
  806. return mSurfaceList[idx];
  807. }
  808. bool D3D9Texture::useDefaultPool()
  809. {
  810. return (mUsage & TU_RENDERTARGET) || (mUsage & TU_DEPTHSTENCIL) || ((mUsage & TU_DYNAMIC) && mDynamicTextures);
  811. }
  812. void D3D9Texture::notifyOnDeviceCreate(IDirect3DDevice9* d3d9Device)
  813. {
  814. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  815. if (D3D9RenderSystem::getResourceManager()->getCreationPolicy() == RCP_CREATE_ON_ALL_DEVICES)
  816. createInternalResources(d3d9Device);
  817. }
  818. void D3D9Texture::notifyOnDeviceDestroy(IDirect3DDevice9* d3d9Device)
  819. {
  820. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  821. auto iterFind = mMapDeviceToTextureResources.find(d3d9Device);
  822. if (iterFind != mMapDeviceToTextureResources.end())
  823. {
  824. TextureResources* textureResource = iterFind->second;
  825. for(unsigned int i = 0; i < mSurfaceList.size(); ++i)
  826. {
  827. D3D9PixelBuffer* pixelBuffer = static_cast<D3D9PixelBuffer*>(mSurfaceList[i].get());
  828. pixelBuffer->destroyBufferResources(d3d9Device);
  829. }
  830. freeTextureResources(d3d9Device, textureResource);
  831. if(textureResource != nullptr)
  832. bs_delete(textureResource);
  833. mMapDeviceToTextureResources.erase(iterFind);
  834. }
  835. }
  836. void D3D9Texture::notifyOnDeviceLost(IDirect3DDevice9* d3d9Device)
  837. {
  838. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  839. if(mD3DPool == D3DPOOL_DEFAULT)
  840. {
  841. auto iterFind = mMapDeviceToTextureResources.find(d3d9Device);
  842. if (iterFind != mMapDeviceToTextureResources.end())
  843. {
  844. TextureResources* textureResource = iterFind->second;
  845. freeTextureResources(d3d9Device, textureResource);
  846. }
  847. }
  848. }
  849. void D3D9Texture::notifyOnDeviceReset(IDirect3DDevice9* d3d9Device)
  850. {
  851. D3D9_DEVICE_ACCESS_CRITICAL_SECTION
  852. if(mD3DPool == D3DPOOL_DEFAULT)
  853. {
  854. createInternalResources(d3d9Device);
  855. }
  856. }
  857. IDirect3DBaseTexture9* D3D9Texture::getTexture_internal()
  858. {
  859. THROW_IF_NOT_CORE_THREAD;
  860. TextureResources* textureResources;
  861. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  862. textureResources = getTextureResources(d3d9Device);
  863. if (textureResources == nullptr || textureResources->pBaseTex == nullptr)
  864. {
  865. createInternalResources(d3d9Device);
  866. textureResources = getTextureResources(d3d9Device);
  867. }
  868. assert(textureResources);
  869. assert(textureResources->pBaseTex);
  870. return textureResources->pBaseTex;
  871. }
  872. IDirect3DTexture9* D3D9Texture::getNormTexture_internal()
  873. {
  874. THROW_IF_NOT_CORE_THREAD;
  875. TextureResources* textureResources;
  876. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  877. textureResources = getTextureResources(d3d9Device);
  878. if (textureResources == nullptr || textureResources->pNormTex == nullptr)
  879. {
  880. createInternalResources(d3d9Device);
  881. textureResources = getTextureResources(d3d9Device);
  882. }
  883. assert(textureResources);
  884. assert(textureResources->pNormTex);
  885. return textureResources->pNormTex;
  886. }
  887. IDirect3DCubeTexture9* D3D9Texture::getCubeTexture_internal()
  888. {
  889. THROW_IF_NOT_CORE_THREAD;
  890. TextureResources* textureResources;
  891. IDirect3DDevice9* d3d9Device = D3D9RenderSystem::getActiveD3D9Device();
  892. textureResources = getTextureResources(d3d9Device);
  893. if (textureResources == nullptr || textureResources->pCubeTex)
  894. {
  895. createInternalResources(d3d9Device);
  896. textureResources = getTextureResources(d3d9Device);
  897. }
  898. assert(textureResources);
  899. assert(textureResources->pCubeTex);
  900. return textureResources->pCubeTex;
  901. }
  902. }