BsD3D9Texture.cpp 37 KB

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