D3D9TextureCube.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "FileSystem.h"
  26. #include "Graphics.h"
  27. #include "GraphicsImpl.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "Renderer.h"
  31. #include "ResourceCache.h"
  32. #include "TextureCube.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. #ifdef _MSC_VER
  36. #pragma warning(disable:4355)
  37. #endif
  38. namespace Urho3D
  39. {
  40. OBJECTTYPESTATIC(TextureCube);
  41. TextureCube::TextureCube(Context* context) :
  42. Texture(context),
  43. lockedLevel_(-1)
  44. {
  45. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  46. faceMemoryUse_[i] = 0;
  47. }
  48. TextureCube::~TextureCube()
  49. {
  50. Release();
  51. }
  52. void TextureCube::RegisterObject(Context* context)
  53. {
  54. context->RegisterFactory<TextureCube>();
  55. }
  56. void TextureCube::OnDeviceLost()
  57. {
  58. if (pool_ == D3DPOOL_DEFAULT)
  59. Release();
  60. }
  61. void TextureCube::OnDeviceReset()
  62. {
  63. if (pool_ == D3DPOOL_DEFAULT || !object_)
  64. {
  65. // If has a file name, reload through the resource cache. Otherwise just recreate.
  66. if (!GetName().Trimmed().Empty())
  67. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  68. else
  69. {
  70. Create();
  71. dataLost_ = true;
  72. }
  73. }
  74. else if (dataPending_)
  75. {
  76. if (!GetName().Trimmed().Empty())
  77. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  78. else
  79. dataLost_ = true;
  80. }
  81. dataPending_ = false;
  82. }
  83. void TextureCube::Release()
  84. {
  85. if (object_)
  86. {
  87. if (!graphics_)
  88. return;
  89. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  90. {
  91. if (graphics_->GetTexture(i) == this)
  92. graphics_->SetTexture(i, 0);
  93. }
  94. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  95. {
  96. if (renderSurfaces_[i])
  97. renderSurfaces_[i]->Release();
  98. }
  99. ((IDirect3DCubeTexture9*)object_)->Release();
  100. object_ = 0;
  101. }
  102. }
  103. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  104. {
  105. if (size <= 0)
  106. {
  107. LOGERROR("Zero or negative cube texture size");
  108. return false;
  109. }
  110. if (usage == TEXTURE_DEPTHSTENCIL)
  111. {
  112. LOGERROR("Depth-stencil usage not supported for cube maps");
  113. return false;
  114. }
  115. // Delete the old rendersurfaces if any
  116. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  117. {
  118. renderSurfaces_[i].Reset();
  119. faceMemoryUse_[i] = 0;
  120. }
  121. pool_ = D3DPOOL_MANAGED;
  122. usage_ = 0;
  123. if (usage == TEXTURE_RENDERTARGET)
  124. {
  125. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  126. renderSurfaces_[i] = new RenderSurface(this);
  127. usage_ |= D3DUSAGE_RENDERTARGET;
  128. pool_ = D3DPOOL_DEFAULT;
  129. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  130. addressMode_[COORD_U] = ADDRESS_CLAMP;
  131. addressMode_[COORD_V] = ADDRESS_CLAMP;
  132. addressMode_[COORD_W] = ADDRESS_CLAMP;
  133. filterMode_ = FILTER_NEAREST;
  134. requestedLevels_ = 1;
  135. }
  136. else if (usage == TEXTURE_DYNAMIC)
  137. {
  138. usage_ |= D3DUSAGE_DYNAMIC;
  139. pool_ = D3DPOOL_DEFAULT;
  140. }
  141. width_ = size;
  142. height_ = size;
  143. format_ = format;
  144. return Create();
  145. }
  146. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  147. {
  148. if (!object_)
  149. {
  150. LOGERROR("No texture created, can not set data");
  151. return false;
  152. }
  153. if (!data)
  154. {
  155. LOGERROR("Null source for setting data");
  156. return false;
  157. }
  158. if (level >= levels_)
  159. {
  160. LOGERROR("Illegal mip level for setting data");
  161. return false;
  162. }
  163. if (graphics_->IsDeviceLost())
  164. {
  165. LOGWARNING("Texture data assignment while device is lost");
  166. dataPending_ = true;
  167. return true;
  168. }
  169. if (IsCompressed())
  170. {
  171. x &= ~3;
  172. y &= ~3;
  173. }
  174. int levelWidth = GetLevelWidth(level);
  175. int levelHeight = GetLevelHeight(level);
  176. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  177. {
  178. LOGERROR("Illegal dimensions for setting data");
  179. return false;
  180. }
  181. D3DLOCKED_RECT d3dLockedRect;
  182. RECT d3dRect;
  183. d3dRect.left = x;
  184. d3dRect.top = y;
  185. d3dRect.right = x + width;
  186. d3dRect.bottom = y + height;
  187. DWORD flags = 0;
  188. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && pool_ == D3DPOOL_DEFAULT)
  189. flags |= D3DLOCK_DISCARD;
  190. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, (flags &
  191. D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  192. {
  193. LOGERROR("Could not lock texture");
  194. return false;
  195. }
  196. if (IsCompressed())
  197. {
  198. height = (height + 3) >> 2;
  199. y >>= 2;
  200. }
  201. unsigned char* src = (unsigned char*)data;
  202. unsigned rowSize = GetRowDataSize(width);
  203. unsigned rowOffset = GetRowDataSize(x);
  204. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  205. if (format_ == D3DFMT_X8R8G8B8)
  206. {
  207. rowSize = rowSize / 3 * 4;
  208. rowOffset = rowOffset / 3 * 4;
  209. }
  210. // Perform conversion from RGB / RGBA as necessary
  211. switch (format_)
  212. {
  213. default:
  214. for (int i = 0; i < height; ++i)
  215. {
  216. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  217. memcpy(dest, src, rowSize);
  218. src += rowSize;
  219. }
  220. break;
  221. case D3DFMT_X8R8G8B8:
  222. for (int i = 0; i < height; ++i)
  223. {
  224. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  225. for (int j = 0; j < width; ++j)
  226. {
  227. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  228. src += 3;
  229. }
  230. }
  231. break;
  232. case D3DFMT_A8R8G8B8:
  233. for (int i = 0; i < height; ++i)
  234. {
  235. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + (y + i) * d3dLockedRect.Pitch + rowOffset;
  236. for (int j = 0; j < width; ++j)
  237. {
  238. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  239. src += 4;
  240. }
  241. }
  242. break;
  243. }
  244. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  245. return true;
  246. }
  247. bool TextureCube::Load(Deserializer& source)
  248. {
  249. PROFILE(LoadTextureCube);
  250. ResourceCache* cache = GetSubsystem<ResourceCache>();
  251. // In headless mode, do not actually load the texture, just return success
  252. Graphics* graphics = GetSubsystem<Graphics>();
  253. if (!graphics)
  254. return true;
  255. // If device is lost, retry later
  256. if (graphics_->IsDeviceLost())
  257. {
  258. LOGWARNING("Texture load while device is lost");
  259. dataPending_ = true;
  260. return true;
  261. }
  262. // If over the texture budget, see if materials can be freed to allow textures to be freed
  263. CheckTextureBudget(GetTypeStatic());
  264. String texPath, texName, texExt;
  265. SplitPath(GetName(), texPath, texName, texExt);
  266. SharedPtr<XMLFile> xml(new XMLFile(context_));
  267. if (!xml->Load(source))
  268. return false;
  269. LoadParameters(xml);
  270. XMLElement textureElem = xml->GetRoot();
  271. XMLElement faceElem = textureElem.GetChild("face");
  272. unsigned faces = 0;
  273. while (faceElem && faces < MAX_CUBEMAP_FACES)
  274. {
  275. String name = faceElem.GetAttribute("name");
  276. String faceTexPath, faceTexName, faceTexExt;
  277. SplitPath(name, faceTexPath, faceTexName, faceTexExt);
  278. // If path is empty, add the XML file path
  279. if (faceTexPath.Empty())
  280. name = texPath + name;
  281. SharedPtr<Image> image(cache->GetResource<Image>(name));
  282. Load((CubeMapFace)faces, image);
  283. faces++;
  284. faceElem = faceElem.GetNext("face");
  285. }
  286. return true;
  287. }
  288. bool TextureCube::Load(CubeMapFace face, Deserializer& source)
  289. {
  290. PROFILE(LoadTextureCube);
  291. SharedPtr<Image> image(new Image(context_));
  292. if (!image->Load(source))
  293. return false;
  294. return Load(face, image);
  295. }
  296. bool TextureCube::Load(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  297. {
  298. if (!image)
  299. {
  300. LOGERROR("Null image, can not load texture");
  301. return false;
  302. }
  303. unsigned memoryUse = 0;
  304. int quality = QUALITY_HIGH;
  305. Renderer* renderer = GetSubsystem<Renderer>();
  306. if (renderer)
  307. quality = renderer->GetTextureQuality();
  308. if (!image->IsCompressed())
  309. {
  310. unsigned char* levelData = image->GetData();
  311. int levelWidth = image->GetWidth();
  312. int levelHeight = image->GetHeight();
  313. unsigned components = image->GetComponents();
  314. unsigned format = 0;
  315. if (levelWidth != levelHeight)
  316. {
  317. LOGERROR("Cube texture width not equal to height");
  318. return false;
  319. }
  320. // Discard unnecessary mip levels
  321. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  322. {
  323. image = image->GetNextLevel();
  324. levelWidth = image->GetWidth();
  325. levelHeight = image->GetHeight();
  326. }
  327. switch (components)
  328. {
  329. case 1:
  330. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  331. break;
  332. case 2:
  333. format = Graphics::GetLuminanceAlphaFormat();
  334. break;
  335. case 3:
  336. format = Graphics::GetRGBFormat();
  337. break;
  338. case 4:
  339. format = Graphics::GetRGBAFormat();
  340. break;
  341. }
  342. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  343. if (!face)
  344. SetSize(levelWidth, format);
  345. else
  346. {
  347. if (!object_)
  348. {
  349. LOGERROR("Cube texture face 0 must be loaded first");
  350. return false;
  351. }
  352. if (levelWidth != width_ || format != format_)
  353. {
  354. LOGERROR("Cube texture face does not match size or format of face 0");
  355. return false;
  356. }
  357. }
  358. for (unsigned i = 0; i < levels_; ++i)
  359. {
  360. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  361. memoryUse += levelWidth * levelHeight * components;
  362. if (i < levels_ - 1)
  363. {
  364. image = image->GetNextLevel();
  365. levelData = image->GetData();
  366. levelWidth = image->GetWidth();
  367. levelHeight = image->GetHeight();
  368. }
  369. }
  370. }
  371. else
  372. {
  373. int width = image->GetWidth();
  374. int height = image->GetHeight();
  375. unsigned levels = image->GetNumCompressedLevels();
  376. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  377. bool needDecompress = false;
  378. if (width != height)
  379. {
  380. LOGERROR("Cube texture width not equal to height");
  381. return false;
  382. }
  383. if (!format)
  384. {
  385. format = Graphics::GetRGBAFormat();
  386. needDecompress = true;
  387. }
  388. unsigned mipsToSkip = mipsToSkip_[quality];
  389. if (mipsToSkip >= levels)
  390. mipsToSkip = levels - 1;
  391. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  392. --mipsToSkip;
  393. width /= (1 << mipsToSkip);
  394. height /= (1 << mipsToSkip);
  395. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  396. if (!face)
  397. {
  398. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  399. SetSize(width, format);
  400. }
  401. else
  402. {
  403. if (!object_)
  404. {
  405. LOGERROR("Cube texture face 0 must be loaded first");
  406. return false;
  407. }
  408. if (width != width_ || format != format_)
  409. {
  410. LOGERROR("Cube texture face does not match size or format of face 0");
  411. return false;
  412. }
  413. }
  414. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  415. {
  416. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  417. if (!needDecompress)
  418. {
  419. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  420. memoryUse += level.rows_ * level.rowSize_;
  421. }
  422. else
  423. {
  424. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  425. level.Decompress(rgbaData);
  426. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  427. memoryUse += level.width_ * level.height_ * 4;
  428. delete[] rgbaData;
  429. }
  430. }
  431. }
  432. faceMemoryUse_[face] = memoryUse;
  433. unsigned totalMemoryUse = sizeof(TextureCube);
  434. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  435. totalMemoryUse += faceMemoryUse_[i];
  436. SetMemoryUse(totalMemoryUse);
  437. return true;
  438. }
  439. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  440. {
  441. if (!object_)
  442. {
  443. LOGERROR("No texture created, can not get data");
  444. return false;
  445. }
  446. if (!dest)
  447. {
  448. LOGERROR("Null destination for getting data");
  449. return false;
  450. }
  451. if (level >= levels_)
  452. {
  453. LOGERROR("Illegal mip level for getting data");
  454. return false;
  455. }
  456. if (graphics_->IsDeviceLost())
  457. {
  458. LOGWARNING("Getting texture data while device is lost");
  459. return false;
  460. }
  461. int levelWidth = GetLevelWidth(level);
  462. int levelHeight = GetLevelHeight(level);
  463. D3DLOCKED_RECT d3dLockedRect;
  464. RECT d3dRect;
  465. d3dRect.left = 0;
  466. d3dRect.top = 0;
  467. d3dRect.right = levelWidth;
  468. d3dRect.bottom = levelHeight;
  469. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  470. {
  471. LOGERROR("Could not lock texture");
  472. return false;
  473. }
  474. int height = levelHeight;
  475. if (IsCompressed())
  476. height = (height + 3) >> 2;
  477. unsigned char* destPtr = (unsigned char*)dest;
  478. unsigned rowSize = GetRowDataSize(levelWidth);
  479. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  480. if (format_ == D3DFMT_X8R8G8B8)
  481. rowSize = rowSize / 3 * 4;
  482. // Perform conversion to RGB / RGBA as necessary
  483. switch (format_)
  484. {
  485. default:
  486. for (int i = 0; i < height; ++i)
  487. {
  488. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  489. memcpy(destPtr, src, rowSize);
  490. destPtr += rowSize;
  491. }
  492. break;
  493. case D3DFMT_X8R8G8B8:
  494. for (int i = 0; i < height; ++i)
  495. {
  496. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  497. for (int j = 0; j < levelWidth; ++j)
  498. {
  499. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; ++src;
  500. destPtr += 3;
  501. }
  502. }
  503. break;
  504. case D3DFMT_A8R8G8B8:
  505. for (int i = 0; i < height; ++i)
  506. {
  507. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  508. for (int j = 0; j < levelWidth; ++j)
  509. {
  510. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; destPtr[3] = *src++;
  511. destPtr += 4;
  512. }
  513. }
  514. break;
  515. }
  516. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  517. return true;
  518. }
  519. bool TextureCube::Create()
  520. {
  521. Release();
  522. if (!graphics_ || !width_ || !height_)
  523. return false;
  524. if (graphics_->IsDeviceLost())
  525. {
  526. LOGWARNING("Texture creation while device is lost");
  527. return true;
  528. }
  529. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  530. if (!device || FAILED(device->CreateCubeTexture(
  531. width_,
  532. requestedLevels_,
  533. usage_,
  534. (D3DFORMAT)format_,
  535. (D3DPOOL)pool_,
  536. (IDirect3DCubeTexture9**)&object_,
  537. 0)))
  538. {
  539. LOGERROR("Could not create cube texture");
  540. return false;
  541. }
  542. levels_ = ((IDirect3DCubeTexture9*)object_)->GetLevelCount();
  543. if (usage_ & D3DUSAGE_RENDERTARGET)
  544. {
  545. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  546. {
  547. ((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  548. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  549. }
  550. }
  551. return true;
  552. }
  553. }