D3D9TextureCube.cpp 19 KB

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