D3D9Texture2D.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Precompiled.h"
  23. #include "../../Core/Context.h"
  24. #include "../../Core/Profiler.h"
  25. #include "../../Graphics/Graphics.h"
  26. #include "../../Graphics/GraphicsEvents.h"
  27. #include "../../Graphics/GraphicsImpl.h"
  28. #include "../../Graphics/Renderer.h"
  29. #include "../../Graphics/Texture2D.h"
  30. #include "../../IO/Log.h"
  31. #include "../../IO/FileSystem.h"
  32. #include "../../Resource/ResourceCache.h"
  33. #include "../../Resource/XMLFile.h"
  34. #include "../../DebugNew.h"
  35. namespace Atomic
  36. {
  37. void Texture2D::OnDeviceLost()
  38. {
  39. if (usage_ > TEXTURE_STATIC)
  40. Release();
  41. }
  42. void Texture2D::OnDeviceReset()
  43. {
  44. if (usage_ > TEXTURE_STATIC || !object_.ptr_ || dataPending_)
  45. {
  46. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  47. ResourceCache* cache = GetSubsystem<ResourceCache>();
  48. if (cache->Exists(GetName()))
  49. dataLost_ = !cache->ReloadResource(this);
  50. if (!object_.ptr_)
  51. {
  52. Create();
  53. dataLost_ = true;
  54. }
  55. }
  56. dataPending_ = false;
  57. }
  58. void Texture2D::Release()
  59. {
  60. if (graphics_)
  61. {
  62. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  63. {
  64. if (graphics_->GetTexture(i) == this)
  65. graphics_->SetTexture(i, 0);
  66. }
  67. }
  68. if (renderSurface_)
  69. renderSurface_->Release();
  70. ATOMIC_SAFE_RELEASE(object_.ptr_);
  71. resolveDirty_ = false;
  72. levelsDirty_ = false;
  73. }
  74. bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data)
  75. {
  76. ATOMIC_PROFILE(SetTextureData);
  77. if (!object_.ptr_)
  78. {
  79. ATOMIC_LOGERROR("No texture created, can not set data");
  80. return false;
  81. }
  82. if (!data)
  83. {
  84. ATOMIC_LOGERROR("Null source for setting data");
  85. return false;
  86. }
  87. if (level >= levels_)
  88. {
  89. ATOMIC_LOGERROR("Illegal mip level for setting data");
  90. return false;
  91. }
  92. if (graphics_->IsDeviceLost())
  93. {
  94. ATOMIC_LOGWARNING("Texture data assignment while device is lost");
  95. dataPending_ = true;
  96. return true;
  97. }
  98. if (IsCompressed())
  99. {
  100. x &= ~3;
  101. y &= ~3;
  102. }
  103. int levelWidth = GetLevelWidth(level);
  104. int levelHeight = GetLevelHeight(level);
  105. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  106. {
  107. ATOMIC_LOGERROR("Illegal dimensions for setting data");
  108. return false;
  109. }
  110. D3DLOCKED_RECT d3dLockedRect;
  111. RECT d3dRect;
  112. d3dRect.left = x;
  113. d3dRect.top = y;
  114. d3dRect.right = x + width;
  115. d3dRect.bottom = y + height;
  116. DWORD flags = 0;
  117. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && usage_ > TEXTURE_STATIC)
  118. flags |= D3DLOCK_DISCARD;
  119. HRESULT hr = ((IDirect3DTexture9*)object_.ptr_)->LockRect(level, &d3dLockedRect, (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags);
  120. if (FAILED(hr))
  121. {
  122. ATOMIC_LOGD3DERROR("Could not lock texture", hr);
  123. return false;
  124. }
  125. if (IsCompressed())
  126. {
  127. height = (height + 3) >> 2;
  128. y >>= 2;
  129. }
  130. unsigned char* src = (unsigned char*)data;
  131. unsigned rowSize = GetRowDataSize(width);
  132. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  133. if (format_ == D3DFMT_X8R8G8B8)
  134. rowSize = rowSize / 3 * 4;
  135. // Perform conversion from RGB / RGBA as necessary
  136. switch (format_)
  137. {
  138. default:
  139. for (int i = 0; i < height; ++i)
  140. {
  141. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  142. memcpy(dest, src, rowSize);
  143. src += rowSize;
  144. }
  145. break;
  146. case D3DFMT_X8R8G8B8:
  147. for (int i = 0; i < height; ++i)
  148. {
  149. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  150. for (int j = 0; j < width; ++j)
  151. {
  152. *dest++ = src[2];
  153. *dest++ = src[1];
  154. *dest++ = src[0];
  155. *dest++ = 255;
  156. src += 3;
  157. }
  158. }
  159. break;
  160. case D3DFMT_A8R8G8B8:
  161. for (int i = 0; i < height; ++i)
  162. {
  163. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  164. for (int j = 0; j < width; ++j)
  165. {
  166. *dest++ = src[2];
  167. *dest++ = src[1];
  168. *dest++ = src[0];
  169. *dest++ = src[3];
  170. src += 4;
  171. }
  172. }
  173. break;
  174. }
  175. ((IDirect3DTexture9*)object_.ptr_)->UnlockRect(level);
  176. return true;
  177. }
  178. bool Texture2D::SetData(Image* image, bool useAlpha)
  179. {
  180. if (!image)
  181. {
  182. ATOMIC_LOGERROR("Null image, can not load texture");
  183. return false;
  184. }
  185. // Use a shared ptr for managing the temporary mip images created during this function
  186. SharedPtr<Image> mipImage;
  187. unsigned memoryUse = sizeof(Texture2D);
  188. int quality = QUALITY_HIGH;
  189. Renderer* renderer = GetSubsystem<Renderer>();
  190. if (renderer)
  191. quality = renderer->GetTextureQuality();
  192. if (!image->IsCompressed())
  193. {
  194. unsigned char* levelData = image->GetData();
  195. int levelWidth = image->GetWidth();
  196. int levelHeight = image->GetHeight();
  197. unsigned components = image->GetComponents();
  198. unsigned format = 0;
  199. // Discard unnecessary mip levels
  200. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  201. {
  202. mipImage = image->GetNextLevel(); image = mipImage;
  203. levelData = image->GetData();
  204. levelWidth = image->GetWidth();
  205. levelHeight = image->GetHeight();
  206. }
  207. switch (components)
  208. {
  209. case 1:
  210. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  211. break;
  212. case 2:
  213. format = Graphics::GetLuminanceAlphaFormat();
  214. break;
  215. case 3:
  216. format = Graphics::GetRGBFormat();
  217. break;
  218. case 4:
  219. format = Graphics::GetRGBAFormat();
  220. break;
  221. default:
  222. assert(false); // Should never reach here
  223. break;
  224. }
  225. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  226. if (IsCompressed() && requestedLevels_ > 1)
  227. requestedLevels_ = 0;
  228. SetSize(levelWidth, levelHeight, format);
  229. for (unsigned i = 0; i < levels_; ++i)
  230. {
  231. SetData(i, 0, 0, levelWidth, levelHeight, levelData);
  232. memoryUse += levelWidth * levelHeight * components;
  233. if (i < levels_ - 1)
  234. {
  235. mipImage = image->GetNextLevel(); image = mipImage;
  236. levelData = image->GetData();
  237. levelWidth = image->GetWidth();
  238. levelHeight = image->GetHeight();
  239. }
  240. }
  241. }
  242. else
  243. {
  244. int width = image->GetWidth();
  245. int height = image->GetHeight();
  246. unsigned levels = image->GetNumCompressedLevels();
  247. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  248. bool needDecompress = false;
  249. if (!format)
  250. {
  251. format = Graphics::GetRGBAFormat();
  252. needDecompress = true;
  253. }
  254. unsigned mipsToSkip = mipsToSkip_[quality];
  255. if (mipsToSkip >= levels)
  256. mipsToSkip = levels - 1;
  257. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  258. --mipsToSkip;
  259. width /= (1 << mipsToSkip);
  260. height /= (1 << mipsToSkip);
  261. SetNumLevels(Max((levels - mipsToSkip), 1U));
  262. SetSize(width, height, format);
  263. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  264. {
  265. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  266. if (!needDecompress)
  267. {
  268. SetData(i, 0, 0, level.width_, level.height_, level.data_);
  269. memoryUse += level.rows_ * level.rowSize_;
  270. }
  271. else
  272. {
  273. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  274. level.Decompress(rgbaData);
  275. SetData(i, 0, 0, level.width_, level.height_, rgbaData);
  276. memoryUse += level.width_ * level.height_ * 4;
  277. delete[] rgbaData;
  278. }
  279. }
  280. }
  281. SetMemoryUse(memoryUse);
  282. return true;
  283. }
  284. bool Texture2D::GetData(unsigned level, void* dest) const
  285. {
  286. if (!object_.ptr_)
  287. {
  288. ATOMIC_LOGERROR("No texture created, can not get data");
  289. return false;
  290. }
  291. if (!dest)
  292. {
  293. ATOMIC_LOGERROR("Null destination for getting data");
  294. return false;
  295. }
  296. if (level >= levels_)
  297. {
  298. ATOMIC_LOGERROR("Illegal mip level for getting data");
  299. return false;
  300. }
  301. if (graphics_->IsDeviceLost())
  302. {
  303. ATOMIC_LOGWARNING("Getting texture data while device is lost");
  304. return false;
  305. }
  306. if (resolveDirty_)
  307. graphics_->ResolveToTexture(const_cast<Texture2D*>(this));
  308. int levelWidth = GetLevelWidth(level);
  309. int levelHeight = GetLevelHeight(level);
  310. D3DLOCKED_RECT d3dLockedRect;
  311. RECT d3dRect;
  312. d3dRect.left = 0;
  313. d3dRect.top = 0;
  314. d3dRect.right = levelWidth;
  315. d3dRect.bottom = levelHeight;
  316. IDirect3DSurface9* offscreenSurface = 0;
  317. // Need to use a offscreen surface & GetRenderTargetData() for rendertargets
  318. if (renderSurface_)
  319. {
  320. if (level != 0)
  321. {
  322. ATOMIC_LOGERROR("Can only get mip level 0 data from a rendertarget");
  323. return false;
  324. }
  325. // If multisampled, must copy the surface of the resolve texture instead of the multisampled surface
  326. IDirect3DSurface9* resolveSurface = 0;
  327. if (multiSample_ > 1)
  328. {
  329. HRESULT hr = ((IDirect3DTexture9*)object_.ptr_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&resolveSurface);
  330. if (FAILED(hr))
  331. {
  332. ATOMIC_LOGD3DERROR("Could not get surface of the resolve texture", hr);
  333. ATOMIC_SAFE_RELEASE(resolveSurface);
  334. return false;
  335. }
  336. }
  337. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  338. HRESULT hr = device->CreateOffscreenPlainSurface((UINT)width_, (UINT)height_, (D3DFORMAT)format_,
  339. D3DPOOL_SYSTEMMEM, &offscreenSurface, 0);
  340. if (FAILED(hr))
  341. {
  342. ATOMIC_LOGD3DERROR("Could not create surface for getting rendertarget data", hr);
  343. ATOMIC_SAFE_RELEASE(offscreenSurface)
  344. ATOMIC_SAFE_RELEASE(resolveSurface);
  345. return false;
  346. }
  347. if (resolveSurface)
  348. hr = device->GetRenderTargetData(resolveSurface, offscreenSurface);
  349. else
  350. hr = device->GetRenderTargetData((IDirect3DSurface9*)renderSurface_->GetSurface(), offscreenSurface);
  351. ATOMIC_SAFE_RELEASE(resolveSurface);
  352. if (FAILED(hr))
  353. {
  354. ATOMIC_LOGD3DERROR("Could not get rendertarget data", hr);
  355. ATOMIC_SAFE_RELEASE(offscreenSurface);
  356. return false;
  357. }
  358. hr = offscreenSurface->LockRect(&d3dLockedRect, &d3dRect, D3DLOCK_READONLY);
  359. if (FAILED(hr))
  360. {
  361. ATOMIC_LOGD3DERROR("Could not lock surface for getting rendertarget data", hr);
  362. ATOMIC_SAFE_RELEASE(offscreenSurface);
  363. return false;
  364. }
  365. }
  366. else
  367. {
  368. HRESULT hr = ((IDirect3DTexture9*)object_.ptr_)->LockRect(level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY);
  369. if (FAILED(hr))
  370. {
  371. ATOMIC_LOGD3DERROR("Could not lock texture", hr);
  372. return false;
  373. }
  374. }
  375. int height = levelHeight;
  376. if (IsCompressed())
  377. height = (height + 3) >> 2;
  378. unsigned char* destPtr = (unsigned char*)dest;
  379. unsigned rowSize = GetRowDataSize(levelWidth);
  380. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  381. if (format_ == D3DFMT_X8R8G8B8)
  382. rowSize = rowSize / 3 * 4;
  383. // Perform conversion to RGB / RGBA as necessary
  384. switch (format_)
  385. {
  386. default:
  387. for (int i = 0; i < height; ++i)
  388. {
  389. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  390. memcpy(destPtr, src, rowSize);
  391. destPtr += rowSize;
  392. }
  393. break;
  394. case D3DFMT_X8R8G8B8:
  395. for (int i = 0; i < height; ++i)
  396. {
  397. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  398. for (int j = 0; j < levelWidth; ++j)
  399. {
  400. destPtr[2] = *src++;
  401. destPtr[1] = *src++;
  402. destPtr[0] = *src++;
  403. ++src;
  404. destPtr += 3;
  405. }
  406. }
  407. break;
  408. case D3DFMT_A8R8G8B8:
  409. for (int i = 0; i < height; ++i)
  410. {
  411. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  412. for (int j = 0; j < levelWidth; ++j)
  413. {
  414. destPtr[2] = *src++;
  415. destPtr[1] = *src++;
  416. destPtr[0] = *src++;
  417. destPtr[3] = *src++;
  418. destPtr += 4;
  419. }
  420. }
  421. break;
  422. }
  423. if (offscreenSurface)
  424. offscreenSurface->UnlockRect();
  425. else
  426. ((IDirect3DTexture9*)object_.ptr_)->UnlockRect(level);
  427. ATOMIC_SAFE_RELEASE(offscreenSurface);
  428. return true;
  429. }
  430. bool Texture2D::Create()
  431. {
  432. Release();
  433. if (!graphics_ || !width_ || !height_)
  434. return false;
  435. if (graphics_->IsDeviceLost())
  436. {
  437. ATOMIC_LOGWARNING("Texture creation while device is lost");
  438. return true;
  439. }
  440. if (multiSample_ > 1 && !autoResolve_)
  441. {
  442. ATOMIC_LOGWARNING("Multisampled texture without autoresolve is not supported on Direct3D9");
  443. autoResolve_ = true;
  444. }
  445. GraphicsImpl* impl = graphics_->GetImpl();
  446. unsigned pool = usage_ > TEXTURE_STATIC ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
  447. unsigned d3dUsage = 0;
  448. switch (usage_)
  449. {
  450. case TEXTURE_DYNAMIC:
  451. d3dUsage |= D3DUSAGE_DYNAMIC;
  452. break;
  453. case TEXTURE_RENDERTARGET:
  454. d3dUsage |= D3DUSAGE_RENDERTARGET;
  455. if (requestedLevels_ != 1)
  456. {
  457. // Check mipmap autogeneration support
  458. if (impl->CheckFormatSupport((D3DFORMAT)format_, D3DUSAGE_AUTOGENMIPMAP, D3DRTYPE_TEXTURE))
  459. {
  460. requestedLevels_ = 0;
  461. d3dUsage |= D3DUSAGE_AUTOGENMIPMAP;
  462. }
  463. else
  464. requestedLevels_ = 1;
  465. }
  466. break;
  467. case TEXTURE_DEPTHSTENCIL:
  468. d3dUsage |= D3DUSAGE_DEPTHSTENCIL;
  469. // No mipmaps for depth-stencil textures
  470. requestedLevels_ = 1;
  471. break;
  472. default:
  473. break;
  474. }
  475. if (multiSample_ > 1)
  476. {
  477. // Fall back to non-multisampled if unsupported multisampling mode
  478. if (!impl->CheckMultiSampleSupport((D3DFORMAT)format_, multiSample_))
  479. {
  480. multiSample_ = 1;
  481. autoResolve_ = false;
  482. }
  483. }
  484. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  485. // If creating a depth-stencil texture, and it is not supported, create a depth-stencil surface instead
  486. // Multisampled surfaces need also to be created this way
  487. if (usage_ == TEXTURE_DEPTHSTENCIL && (multiSample_ > 1 || !graphics_->GetImpl()->CheckFormatSupport((D3DFORMAT)format_,
  488. d3dUsage, D3DRTYPE_TEXTURE)))
  489. {
  490. HRESULT hr = device->CreateDepthStencilSurface(
  491. (UINT)width_,
  492. (UINT)height_,
  493. (D3DFORMAT)format_,
  494. (multiSample_ > 1) ? (D3DMULTISAMPLE_TYPE)multiSample_ : D3DMULTISAMPLE_NONE,
  495. 0,
  496. FALSE,
  497. (IDirect3DSurface9**)&renderSurface_->surface_,
  498. 0);
  499. if (FAILED(hr))
  500. {
  501. ATOMIC_LOGD3DERROR("Could not create depth-stencil surface", hr);
  502. ATOMIC_SAFE_RELEASE(renderSurface_->surface_);
  503. return false;
  504. }
  505. levels_ = 1;
  506. }
  507. else
  508. {
  509. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateTexture(
  510. (UINT)width_,
  511. (UINT)height_,
  512. requestedLevels_,
  513. d3dUsage,
  514. (D3DFORMAT)format_,
  515. (D3DPOOL)pool,
  516. (IDirect3DTexture9**)&object_,
  517. 0);
  518. if (FAILED(hr))
  519. {
  520. ATOMIC_LOGD3DERROR("Could not create texture", hr);
  521. ATOMIC_SAFE_RELEASE(object_.ptr_);
  522. return false;
  523. }
  524. levels_ = ((IDirect3DTexture9*)object_.ptr_)->GetLevelCount();
  525. // Create the multisampled rendertarget for rendering to if necessary
  526. if (usage_ == TEXTURE_RENDERTARGET && multiSample_ > 1)
  527. {
  528. HRESULT hr = device->CreateRenderTarget(
  529. (UINT)width_,
  530. (UINT)height_,
  531. (D3DFORMAT)format_,
  532. (D3DMULTISAMPLE_TYPE)multiSample_,
  533. 0,
  534. FALSE,
  535. (IDirect3DSurface9**)&renderSurface_->surface_,
  536. 0);
  537. if (FAILED(hr))
  538. {
  539. ATOMIC_LOGD3DERROR("Could not create multisampled rendertarget surface", hr);
  540. ATOMIC_SAFE_RELEASE(renderSurface_->surface_);
  541. return false;
  542. }
  543. }
  544. else if (usage_ >= TEXTURE_RENDERTARGET)
  545. {
  546. // Else use the texture surface directly for rendering
  547. hr = ((IDirect3DTexture9*)object_.ptr_)->GetSurfaceLevel(0, (IDirect3DSurface9**)&renderSurface_->surface_);
  548. if (FAILED(hr))
  549. {
  550. ATOMIC_LOGD3DERROR("Could not get rendertarget surface", hr);
  551. ATOMIC_SAFE_RELEASE(renderSurface_->surface_);
  552. return false;
  553. }
  554. }
  555. }
  556. return true;
  557. }
  558. }