D3D9TextureCube.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. //
  2. // Copyright (c) 2008-2016 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/TextureCube.h"
  30. #include "../../IO/FileSystem.h"
  31. #include "../../IO/Log.h"
  32. #include "../../Resource/ResourceCache.h"
  33. #include "../../Resource/XMLFile.h"
  34. #include "../../DebugNew.h"
  35. #ifdef _MSC_VER
  36. #pragma warning(disable:4355)
  37. #endif
  38. namespace Atomic
  39. {
  40. void TextureCube::OnDeviceLost()
  41. {
  42. if (usage_ > TEXTURE_STATIC)
  43. Release();
  44. }
  45. void TextureCube::OnDeviceReset()
  46. {
  47. if (usage_ > TEXTURE_STATIC || !object_.ptr_ || dataPending_)
  48. {
  49. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  50. ResourceCache* cache = GetSubsystem<ResourceCache>();
  51. if (cache->Exists(GetName()))
  52. dataLost_ = !cache->ReloadResource(this);
  53. if (!object_.ptr_)
  54. {
  55. Create();
  56. dataLost_ = true;
  57. }
  58. }
  59. dataPending_ = false;
  60. }
  61. void TextureCube::Release()
  62. {
  63. if (graphics_)
  64. {
  65. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  66. {
  67. if (graphics_->GetTexture(i) == this)
  68. graphics_->SetTexture(i, 0);
  69. }
  70. }
  71. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  72. {
  73. if (renderSurfaces_[i])
  74. renderSurfaces_[i]->Release();
  75. }
  76. ATOMIC_SAFE_RELEASE(object_.ptr_);
  77. }
  78. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  79. {
  80. ATOMIC_PROFILE(SetTextureData);
  81. if (!object_.ptr_)
  82. {
  83. ATOMIC_LOGERROR("No texture created, can not set data");
  84. return false;
  85. }
  86. if (!data)
  87. {
  88. ATOMIC_LOGERROR("Null source for setting data");
  89. return false;
  90. }
  91. if (level >= levels_)
  92. {
  93. ATOMIC_LOGERROR("Illegal mip level for setting data");
  94. return false;
  95. }
  96. if (graphics_->IsDeviceLost())
  97. {
  98. ATOMIC_LOGWARNING("Texture data assignment while device is lost");
  99. dataPending_ = true;
  100. return true;
  101. }
  102. if (IsCompressed())
  103. {
  104. x &= ~3;
  105. y &= ~3;
  106. }
  107. int levelWidth = GetLevelWidth(level);
  108. int levelHeight = GetLevelHeight(level);
  109. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  110. {
  111. ATOMIC_LOGERROR("Illegal dimensions for setting data");
  112. return false;
  113. }
  114. D3DLOCKED_RECT d3dLockedRect;
  115. RECT d3dRect;
  116. d3dRect.left = x;
  117. d3dRect.top = y;
  118. d3dRect.right = x + width;
  119. d3dRect.bottom = y + height;
  120. DWORD flags = 0;
  121. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && usage_ > TEXTURE_STATIC)
  122. flags |= D3DLOCK_DISCARD;
  123. HRESULT hr = ((IDirect3DCubeTexture9*)object_.ptr_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect,
  124. (flags & D3DLOCK_DISCARD) ? 0 : &d3dRect, flags);
  125. if (FAILED(hr))
  126. {
  127. ATOMIC_LOGD3DERROR("Could not lock texture", hr);
  128. return false;
  129. }
  130. if (IsCompressed())
  131. {
  132. height = (height + 3) >> 2;
  133. y >>= 2;
  134. }
  135. unsigned char* src = (unsigned char*)data;
  136. unsigned rowSize = GetRowDataSize(width);
  137. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  138. if (format_ == D3DFMT_X8R8G8B8)
  139. rowSize = rowSize / 3 * 4;
  140. // Perform conversion from RGB / RGBA as necessary
  141. switch (format_)
  142. {
  143. default:
  144. for (int i = 0; i < height; ++i)
  145. {
  146. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  147. memcpy(dest, src, rowSize);
  148. src += rowSize;
  149. }
  150. break;
  151. case D3DFMT_X8R8G8B8:
  152. for (int i = 0; i < height; ++i)
  153. {
  154. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  155. for (int j = 0; j < width; ++j)
  156. {
  157. *dest++ = src[2];
  158. *dest++ = src[1];
  159. *dest++ = src[0];
  160. *dest++ = 255;
  161. src += 3;
  162. }
  163. }
  164. break;
  165. case D3DFMT_A8R8G8B8:
  166. for (int i = 0; i < height; ++i)
  167. {
  168. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  169. for (int j = 0; j < width; ++j)
  170. {
  171. *dest++ = src[2];
  172. *dest++ = src[1];
  173. *dest++ = src[0];
  174. *dest++ = src[3];
  175. src += 4;
  176. }
  177. }
  178. break;
  179. }
  180. ((IDirect3DCubeTexture9*)object_.ptr_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  181. return true;
  182. }
  183. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  184. {
  185. SharedPtr<Image> image(new Image(context_));
  186. if (!image->Load(source))
  187. return false;
  188. return SetData(face, image);
  189. }
  190. bool TextureCube::SetData(CubeMapFace face, Image* image, bool useAlpha)
  191. {
  192. if (!image)
  193. {
  194. ATOMIC_LOGERROR("Null image, can not load texture");
  195. return false;
  196. }
  197. // Use a shared ptr for managing the temporary mip images created during this function
  198. SharedPtr<Image> mipImage;
  199. unsigned memoryUse = 0;
  200. int quality = QUALITY_HIGH;
  201. Renderer* renderer = GetSubsystem<Renderer>();
  202. if (renderer)
  203. quality = renderer->GetTextureQuality();
  204. if (!image->IsCompressed())
  205. {
  206. unsigned char* levelData = image->GetData();
  207. int levelWidth = image->GetWidth();
  208. int levelHeight = image->GetHeight();
  209. unsigned components = image->GetComponents();
  210. unsigned format = 0;
  211. if (levelWidth != levelHeight)
  212. {
  213. ATOMIC_LOGERROR("Cube texture width not equal to height");
  214. return false;
  215. }
  216. // Discard unnecessary mip levels
  217. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  218. {
  219. mipImage = image->GetNextLevel(); image = mipImage;
  220. levelData = image->GetData();
  221. levelWidth = image->GetWidth();
  222. levelHeight = image->GetHeight();
  223. }
  224. switch (components)
  225. {
  226. case 1:
  227. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  228. break;
  229. case 2:
  230. format = Graphics::GetLuminanceAlphaFormat();
  231. break;
  232. case 3:
  233. format = Graphics::GetRGBFormat();
  234. break;
  235. case 4:
  236. format = Graphics::GetRGBAFormat();
  237. break;
  238. default:
  239. assert(false); // Should never reach here
  240. break;
  241. }
  242. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  243. if (!face)
  244. {
  245. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  246. if (IsCompressed() && requestedLevels_ > 1)
  247. requestedLevels_ = 0;
  248. SetSize(levelWidth, format);
  249. }
  250. else
  251. {
  252. if (!object_.ptr_)
  253. {
  254. ATOMIC_LOGERROR("Cube texture face 0 must be loaded first");
  255. return false;
  256. }
  257. if (levelWidth != width_ || format != format_)
  258. {
  259. ATOMIC_LOGERROR("Cube texture face does not match size or format of face 0");
  260. return false;
  261. }
  262. }
  263. for (unsigned i = 0; i < levels_; ++i)
  264. {
  265. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  266. memoryUse += levelWidth * levelHeight * components;
  267. if (i < levels_ - 1)
  268. {
  269. mipImage = image->GetNextLevel(); image = mipImage;
  270. levelData = image->GetData();
  271. levelWidth = image->GetWidth();
  272. levelHeight = image->GetHeight();
  273. }
  274. }
  275. }
  276. else
  277. {
  278. int width = image->GetWidth();
  279. int height = image->GetHeight();
  280. unsigned levels = image->GetNumCompressedLevels();
  281. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  282. bool needDecompress = false;
  283. if (width != height)
  284. {
  285. ATOMIC_LOGERROR("Cube texture width not equal to height");
  286. return false;
  287. }
  288. if (!format)
  289. {
  290. format = Graphics::GetRGBAFormat();
  291. needDecompress = true;
  292. }
  293. unsigned mipsToSkip = mipsToSkip_[quality];
  294. if (mipsToSkip >= levels)
  295. mipsToSkip = levels - 1;
  296. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  297. --mipsToSkip;
  298. width /= (1 << mipsToSkip);
  299. height /= (1 << mipsToSkip);
  300. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  301. if (!face)
  302. {
  303. SetNumLevels(Max((levels - mipsToSkip), 1U));
  304. SetSize(width, format);
  305. }
  306. else
  307. {
  308. if (!object_.ptr_)
  309. {
  310. ATOMIC_LOGERROR("Cube texture face 0 must be loaded first");
  311. return false;
  312. }
  313. if (width != width_ || format != format_)
  314. {
  315. ATOMIC_LOGERROR("Cube texture face does not match size or format of face 0");
  316. return false;
  317. }
  318. }
  319. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  320. {
  321. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  322. if (!needDecompress)
  323. {
  324. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  325. memoryUse += level.rows_ * level.rowSize_;
  326. }
  327. else
  328. {
  329. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  330. level.Decompress(rgbaData);
  331. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  332. memoryUse += level.width_ * level.height_ * 4;
  333. delete[] rgbaData;
  334. }
  335. }
  336. }
  337. faceMemoryUse_[face] = memoryUse;
  338. unsigned totalMemoryUse = sizeof(TextureCube);
  339. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  340. totalMemoryUse += faceMemoryUse_[i];
  341. SetMemoryUse(totalMemoryUse);
  342. return true;
  343. }
  344. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  345. {
  346. if (!object_.ptr_)
  347. {
  348. ATOMIC_LOGERROR("No texture created, can not get data");
  349. return false;
  350. }
  351. if (!dest)
  352. {
  353. ATOMIC_LOGERROR("Null destination for getting data");
  354. return false;
  355. }
  356. if (level >= levels_)
  357. {
  358. ATOMIC_LOGERROR("Illegal mip level for getting data");
  359. return false;
  360. }
  361. if (graphics_->IsDeviceLost())
  362. {
  363. ATOMIC_LOGWARNING("Getting texture data while device is lost");
  364. return false;
  365. }
  366. int levelWidth = GetLevelWidth(level);
  367. int levelHeight = GetLevelHeight(level);
  368. D3DLOCKED_RECT d3dLockedRect;
  369. RECT d3dRect;
  370. d3dRect.left = 0;
  371. d3dRect.top = 0;
  372. d3dRect.right = levelWidth;
  373. d3dRect.bottom = levelHeight;
  374. IDirect3DSurface9* offscreenSurface = 0;
  375. // Need to use a offscreen surface & GetRenderTargetData() for rendertargets
  376. if (renderSurfaces_[face])
  377. {
  378. if (level != 0)
  379. {
  380. ATOMIC_LOGERROR("Can only get mip level 0 data from a rendertarget");
  381. return false;
  382. }
  383. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  384. HRESULT hr = device->CreateOffscreenPlainSurface((UINT)width_, (UINT)height_, (D3DFORMAT)format_, D3DPOOL_SYSTEMMEM, &offscreenSurface, 0);
  385. if (FAILED(hr))
  386. {
  387. ATOMIC_SAFE_RELEASE(offscreenSurface);
  388. ATOMIC_LOGD3DERROR("Could not create surface for getting rendertarget data", hr);
  389. return false;
  390. }
  391. hr = device->GetRenderTargetData((IDirect3DSurface9*)renderSurfaces_[face]->GetSurface(), offscreenSurface);
  392. if (FAILED(hr))
  393. {
  394. ATOMIC_LOGD3DERROR("Could not get rendertarget data", hr);
  395. offscreenSurface->Release();
  396. return false;
  397. }
  398. if (FAILED(offscreenSurface->LockRect(&d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  399. {
  400. ATOMIC_LOGD3DERROR("Could not lock surface for getting rendertarget data", hr);
  401. offscreenSurface->Release();
  402. return false;
  403. }
  404. }
  405. else
  406. {
  407. HRESULT hr = ((IDirect3DCubeTexture9*)object_.ptr_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY);
  408. if (FAILED(hr))
  409. {
  410. ATOMIC_LOGD3DERROR("Could not lock texture", hr);
  411. return false;
  412. }
  413. }
  414. int height = levelHeight;
  415. if (IsCompressed())
  416. height = (height + 3) >> 2;
  417. unsigned char* destPtr = (unsigned char*)dest;
  418. unsigned rowSize = GetRowDataSize(levelWidth);
  419. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  420. if (format_ == D3DFMT_X8R8G8B8)
  421. rowSize = rowSize / 3 * 4;
  422. // Perform conversion to RGB / RGBA as necessary
  423. switch (format_)
  424. {
  425. default:
  426. for (int i = 0; i < height; ++i)
  427. {
  428. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  429. memcpy(destPtr, src, rowSize);
  430. destPtr += rowSize;
  431. }
  432. break;
  433. case D3DFMT_X8R8G8B8:
  434. for (int i = 0; i < height; ++i)
  435. {
  436. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  437. for (int j = 0; j < levelWidth; ++j)
  438. {
  439. destPtr[2] = *src++;
  440. destPtr[1] = *src++;
  441. destPtr[0] = *src++;
  442. ++src;
  443. destPtr += 3;
  444. }
  445. }
  446. break;
  447. case D3DFMT_A8R8G8B8:
  448. for (int i = 0; i < height; ++i)
  449. {
  450. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  451. for (int j = 0; j < levelWidth; ++j)
  452. {
  453. destPtr[2] = *src++;
  454. destPtr[1] = *src++;
  455. destPtr[0] = *src++;
  456. destPtr[3] = *src++;
  457. destPtr += 4;
  458. }
  459. }
  460. break;
  461. }
  462. if (offscreenSurface)
  463. {
  464. offscreenSurface->UnlockRect();
  465. offscreenSurface->Release();
  466. }
  467. else
  468. ((IDirect3DCubeTexture9*)object_.ptr_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  469. return true;
  470. }
  471. bool TextureCube::Create()
  472. {
  473. Release();
  474. if (!graphics_ || !width_ || !height_)
  475. return false;
  476. if (graphics_->IsDeviceLost())
  477. {
  478. ATOMIC_LOGWARNING("Texture creation while device is lost");
  479. return true;
  480. }
  481. unsigned pool = usage_ > TEXTURE_STATIC ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED;
  482. unsigned d3dUsage = 0;
  483. switch (usage_)
  484. {
  485. case TEXTURE_DYNAMIC:
  486. d3dUsage |= D3DUSAGE_DYNAMIC;
  487. break;
  488. case TEXTURE_RENDERTARGET:
  489. d3dUsage |= D3DUSAGE_RENDERTARGET;
  490. break;
  491. default:
  492. break;
  493. }
  494. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  495. HRESULT hr = device->CreateCubeTexture(
  496. (UINT)width_,
  497. requestedLevels_,
  498. d3dUsage,
  499. (D3DFORMAT)format_,
  500. (D3DPOOL)pool,
  501. (IDirect3DCubeTexture9**)&object_.ptr_,
  502. 0);
  503. if (FAILED(hr))
  504. {
  505. ATOMIC_SAFE_RELEASE(object_.ptr_);
  506. ATOMIC_LOGD3DERROR("Could not create cube texture", hr);
  507. return false;
  508. }
  509. levels_ = ((IDirect3DCubeTexture9*)object_.ptr_)->GetLevelCount();
  510. if (usage_ == TEXTURE_RENDERTARGET)
  511. {
  512. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  513. {
  514. hr = ((IDirect3DCubeTexture9*)object_.ptr_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  515. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  516. if (FAILED(hr))
  517. ATOMIC_LOGD3DERROR("Could not get rendertarget surface", hr);
  518. }
  519. }
  520. return true;
  521. }
  522. }