BsD3D9Texture.cpp 38 KB

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