D3D9TextureCube.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "FileSystem.h"
  25. #include "Graphics.h"
  26. #include "GraphicsEvents.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. static const char* cubeMapLayoutNames[] = {
  41. "horizontal",
  42. "horizontalnvidia",
  43. "horizontalcross",
  44. "verticalcross",
  45. "blender",
  46. 0
  47. };
  48. static SharedPtr<Image> GetTileImage(Image* src, int tileX, int tileY, int tileWidth, int tileHeight)
  49. {
  50. return SharedPtr<Image>(src->GetSubimage(IntRect(tileX * tileWidth, tileY * tileHeight, (tileX + 1) * tileWidth, (tileY + 1) * tileHeight)));
  51. }
  52. TextureCube::TextureCube(Context* context) :
  53. Texture(context),
  54. lockedLevel_(-1)
  55. {
  56. // Default to clamp mode addressing
  57. addressMode_[COORD_U] = ADDRESS_CLAMP;
  58. addressMode_[COORD_V] = ADDRESS_CLAMP;
  59. addressMode_[COORD_W] = ADDRESS_CLAMP;
  60. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  61. faceMemoryUse_[i] = 0;
  62. }
  63. TextureCube::~TextureCube()
  64. {
  65. Release();
  66. }
  67. void TextureCube::RegisterObject(Context* context)
  68. {
  69. context->RegisterFactory<TextureCube>();
  70. }
  71. bool TextureCube::BeginLoad(Deserializer& source)
  72. {
  73. ResourceCache* cache = GetSubsystem<ResourceCache>();
  74. // In headless mode, do not actually load the texture, just return success
  75. if (!graphics_)
  76. return true;
  77. // If device is lost, retry later
  78. if (graphics_->IsDeviceLost())
  79. {
  80. LOGWARNING("Texture load while device is lost");
  81. dataPending_ = true;
  82. return true;
  83. }
  84. cache->ResetDependencies(this);
  85. String texPath, texName, texExt;
  86. SplitPath(GetName(), texPath, texName, texExt);
  87. loadParameters_ = (new XMLFile(context_));
  88. if (!loadParameters_->Load(source))
  89. {
  90. loadParameters_.Reset();
  91. return false;
  92. }
  93. loadImages_.Clear();
  94. XMLElement textureElem = loadParameters_->GetRoot();
  95. XMLElement imageElem = textureElem.GetChild("image");
  96. // Single image and multiple faces with layout
  97. if (imageElem)
  98. {
  99. String name = imageElem.GetAttribute("name");
  100. // If path is empty, add the XML file path
  101. if (GetPath(name).Empty())
  102. name = texPath + name;
  103. CubeMapLayout layout = (CubeMapLayout)GetStringListIndex(imageElem.GetAttribute("layout").CString(), cubeMapLayoutNames, CML_HORIZONTAL);
  104. SharedPtr<Image> image = cache->GetTempResource<Image>(name);
  105. if (!image)
  106. return false;
  107. int faceWidth, faceHeight;
  108. loadImages_.Resize(MAX_CUBEMAP_FACES);
  109. switch (layout)
  110. {
  111. case CML_HORIZONTAL:
  112. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  113. faceHeight = image->GetHeight();
  114. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  115. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  116. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  117. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 3, 0, faceWidth, faceHeight);
  118. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 4, 0, faceWidth, faceHeight);
  119. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 5, 0, faceWidth, faceHeight);
  120. break;
  121. case CML_HORIZONTALNVIDIA:
  122. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  123. faceHeight = image->GetHeight();
  124. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  125. loadImages_[i] = GetTileImage(image, i, 0, faceWidth, faceHeight);
  126. break;
  127. case CML_HORIZONTALCROSS:
  128. faceWidth = image->GetWidth() / 4;
  129. faceHeight = image->GetHeight() / 3;
  130. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  131. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  132. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  133. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  134. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 3, 1, faceWidth, faceHeight);
  135. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  136. break;
  137. case CML_VERTICALCROSS:
  138. faceWidth = image->GetWidth() / 3;
  139. faceHeight = image->GetHeight() / 4;
  140. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  141. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  142. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  143. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  144. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  145. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 3, faceWidth, faceHeight);
  146. if (loadImages_[FACE_NEGATIVE_Z])
  147. {
  148. loadImages_[FACE_NEGATIVE_Z]->FlipVertical();
  149. loadImages_[FACE_NEGATIVE_Z]->FlipHorizontal();
  150. }
  151. break;
  152. case CML_BLENDER:
  153. faceWidth = image->GetWidth() / 3;
  154. faceHeight = image->GetHeight() / 2;
  155. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  156. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  157. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  158. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  159. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  160. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  161. break;
  162. }
  163. }
  164. // Face per image
  165. else
  166. {
  167. XMLElement faceElem = textureElem.GetChild("face");
  168. while (faceElem)
  169. {
  170. String name = faceElem.GetAttribute("name");
  171. // If path is empty, add the XML file path
  172. if (GetPath(name).Empty())
  173. name = texPath + name;
  174. loadImages_.Push(cache->GetTempResource<Image>(name));
  175. cache->StoreResourceDependency(this, name);
  176. faceElem = faceElem.GetNext("face");
  177. }
  178. }
  179. // Precalculate mip levels if async loading
  180. if (GetAsyncLoadState() == ASYNC_LOADING)
  181. {
  182. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  183. {
  184. if (loadImages_[i])
  185. loadImages_[i]->PrecalculateLevels();
  186. }
  187. }
  188. return true;
  189. }
  190. bool TextureCube::EndLoad()
  191. {
  192. // In headless mode, do not actually load the texture, just return success
  193. if (!graphics_ || graphics_->IsDeviceLost())
  194. return true;
  195. // If over the texture budget, see if materials can be freed to allow textures to be freed
  196. CheckTextureBudget(GetTypeStatic());
  197. SetParameters(loadParameters_);
  198. for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
  199. SetData((CubeMapFace)i, loadImages_[i]);
  200. loadImages_.Clear();
  201. loadParameters_.Reset();
  202. return true;
  203. }
  204. void TextureCube::OnDeviceLost()
  205. {
  206. if (pool_ == D3DPOOL_DEFAULT)
  207. Release();
  208. }
  209. void TextureCube::OnDeviceReset()
  210. {
  211. if (pool_ == D3DPOOL_DEFAULT || !object_ || dataPending_)
  212. {
  213. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  214. ResourceCache* cache = GetSubsystem<ResourceCache>();
  215. if (cache->Exists(GetName()))
  216. dataLost_ = !cache->ReloadResource(this);
  217. if (!object_)
  218. {
  219. Create();
  220. dataLost_ = true;
  221. }
  222. }
  223. dataPending_ = false;
  224. }
  225. void TextureCube::Release()
  226. {
  227. if (object_)
  228. {
  229. if (!graphics_)
  230. return;
  231. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  232. {
  233. if (graphics_->GetTexture(i) == this)
  234. graphics_->SetTexture(i, 0);
  235. }
  236. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  237. {
  238. if (renderSurfaces_[i])
  239. renderSurfaces_[i]->Release();
  240. }
  241. ((IDirect3DCubeTexture9*)object_)->Release();
  242. object_ = 0;
  243. }
  244. }
  245. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  246. {
  247. if (size <= 0)
  248. {
  249. LOGERROR("Zero or negative cube texture size");
  250. return false;
  251. }
  252. if (usage == TEXTURE_DEPTHSTENCIL)
  253. {
  254. LOGERROR("Depth-stencil usage not supported for cube maps");
  255. return false;
  256. }
  257. // Delete the old rendersurfaces if any
  258. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  259. {
  260. renderSurfaces_[i].Reset();
  261. faceMemoryUse_[i] = 0;
  262. }
  263. pool_ = D3DPOOL_MANAGED;
  264. usage_ = 0;
  265. if (usage == TEXTURE_RENDERTARGET)
  266. {
  267. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  268. renderSurfaces_[i] = new RenderSurface(this);
  269. usage_ |= D3DUSAGE_RENDERTARGET;
  270. pool_ = D3DPOOL_DEFAULT;
  271. // Nearest filtering and mipmaps disabled by default
  272. filterMode_ = FILTER_NEAREST;
  273. requestedLevels_ = 1;
  274. }
  275. else if (usage == TEXTURE_DYNAMIC)
  276. {
  277. usage_ |= D3DUSAGE_DYNAMIC;
  278. pool_ = D3DPOOL_DEFAULT;
  279. }
  280. if (usage == TEXTURE_RENDERTARGET)
  281. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(TextureCube, HandleRenderSurfaceUpdate));
  282. else
  283. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  284. width_ = size;
  285. height_ = size;
  286. format_ = format;
  287. return Create();
  288. }
  289. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  290. {
  291. PROFILE(SetTextureData);
  292. if (!object_)
  293. {
  294. LOGERROR("No texture created, can not set data");
  295. return false;
  296. }
  297. if (!data)
  298. {
  299. LOGERROR("Null source for setting data");
  300. return false;
  301. }
  302. if (level >= levels_)
  303. {
  304. LOGERROR("Illegal mip level for setting data");
  305. return false;
  306. }
  307. if (graphics_->IsDeviceLost())
  308. {
  309. LOGWARNING("Texture data assignment while device is lost");
  310. dataPending_ = true;
  311. return true;
  312. }
  313. if (IsCompressed())
  314. {
  315. x &= ~3;
  316. y &= ~3;
  317. }
  318. int levelWidth = GetLevelWidth(level);
  319. int levelHeight = GetLevelHeight(level);
  320. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  321. {
  322. LOGERROR("Illegal dimensions for setting data");
  323. return false;
  324. }
  325. D3DLOCKED_RECT d3dLockedRect;
  326. RECT d3dRect;
  327. d3dRect.left = x;
  328. d3dRect.top = y;
  329. d3dRect.right = x + width;
  330. d3dRect.bottom = y + height;
  331. DWORD flags = 0;
  332. if (level == 0 && x == 0 && y == 0 && width == levelWidth && height == levelHeight && pool_ == D3DPOOL_DEFAULT)
  333. flags |= D3DLOCK_DISCARD;
  334. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, (flags &
  335. D3DLOCK_DISCARD) ? 0 : &d3dRect, flags)))
  336. {
  337. LOGERROR("Could not lock texture");
  338. return false;
  339. }
  340. if (IsCompressed())
  341. {
  342. height = (height + 3) >> 2;
  343. y >>= 2;
  344. }
  345. unsigned char* src = (unsigned char*)data;
  346. unsigned rowSize = GetRowDataSize(width);
  347. // GetRowDataSize() returns CPU-side (source) data size, so need to convert for X8R8G8B8
  348. if (format_ == D3DFMT_X8R8G8B8)
  349. rowSize = rowSize / 3 * 4;
  350. // Perform conversion from RGB / RGBA as necessary
  351. switch (format_)
  352. {
  353. default:
  354. for (int i = 0; i < height; ++i)
  355. {
  356. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  357. memcpy(dest, src, rowSize);
  358. src += rowSize;
  359. }
  360. break;
  361. case D3DFMT_X8R8G8B8:
  362. for (int i = 0; i < height; ++i)
  363. {
  364. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  365. for (int j = 0; j < width; ++j)
  366. {
  367. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = 255;
  368. src += 3;
  369. }
  370. }
  371. break;
  372. case D3DFMT_A8R8G8B8:
  373. for (int i = 0; i < height; ++i)
  374. {
  375. unsigned char* dest = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  376. for (int j = 0; j < width; ++j)
  377. {
  378. *dest++ = src[2]; *dest++ = src[1]; *dest++ = src[0]; *dest++ = src[3];
  379. src += 4;
  380. }
  381. }
  382. break;
  383. }
  384. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  385. return true;
  386. }
  387. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  388. {
  389. SharedPtr<Image> image(new Image(context_));
  390. if (!image->Load(source))
  391. return false;
  392. return SetData(face, image);
  393. }
  394. bool TextureCube::SetData(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  395. {
  396. if (!image)
  397. {
  398. LOGERROR("Null image, can not load texture");
  399. return false;
  400. }
  401. unsigned memoryUse = 0;
  402. int quality = QUALITY_HIGH;
  403. Renderer* renderer = GetSubsystem<Renderer>();
  404. if (renderer)
  405. quality = renderer->GetTextureQuality();
  406. if (!image->IsCompressed())
  407. {
  408. unsigned char* levelData = image->GetData();
  409. int levelWidth = image->GetWidth();
  410. int levelHeight = image->GetHeight();
  411. unsigned components = image->GetComponents();
  412. unsigned format = 0;
  413. if (levelWidth != levelHeight)
  414. {
  415. LOGERROR("Cube texture width not equal to height");
  416. return false;
  417. }
  418. // Discard unnecessary mip levels
  419. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  420. {
  421. image = image->GetNextLevel();
  422. levelData = image->GetData();
  423. levelWidth = image->GetWidth();
  424. levelHeight = image->GetHeight();
  425. }
  426. switch (components)
  427. {
  428. case 1:
  429. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  430. break;
  431. case 2:
  432. format = Graphics::GetLuminanceAlphaFormat();
  433. break;
  434. case 3:
  435. format = Graphics::GetRGBFormat();
  436. break;
  437. case 4:
  438. format = Graphics::GetRGBAFormat();
  439. break;
  440. }
  441. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  442. if (!face)
  443. {
  444. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  445. if (IsCompressed() && requestedLevels_ > 1)
  446. requestedLevels_ = 0;
  447. SetSize(levelWidth, format);
  448. }
  449. else
  450. {
  451. if (!object_)
  452. {
  453. LOGERROR("Cube texture face 0 must be loaded first");
  454. return false;
  455. }
  456. if (levelWidth != width_ || format != format_)
  457. {
  458. LOGERROR("Cube texture face does not match size or format of face 0");
  459. return false;
  460. }
  461. }
  462. for (unsigned i = 0; i < levels_; ++i)
  463. {
  464. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  465. memoryUse += levelWidth * levelHeight * components;
  466. if (i < levels_ - 1)
  467. {
  468. image = image->GetNextLevel();
  469. levelData = image->GetData();
  470. levelWidth = image->GetWidth();
  471. levelHeight = image->GetHeight();
  472. }
  473. }
  474. }
  475. else
  476. {
  477. int width = image->GetWidth();
  478. int height = image->GetHeight();
  479. unsigned levels = image->GetNumCompressedLevels();
  480. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  481. bool needDecompress = false;
  482. if (width != height)
  483. {
  484. LOGERROR("Cube texture width not equal to height");
  485. return false;
  486. }
  487. if (!format)
  488. {
  489. format = Graphics::GetRGBAFormat();
  490. needDecompress = true;
  491. }
  492. unsigned mipsToSkip = mipsToSkip_[quality];
  493. if (mipsToSkip >= levels)
  494. mipsToSkip = levels - 1;
  495. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  496. --mipsToSkip;
  497. width /= (1 << mipsToSkip);
  498. height /= (1 << mipsToSkip);
  499. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  500. if (!face)
  501. {
  502. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  503. SetSize(width, format);
  504. }
  505. else
  506. {
  507. if (!object_)
  508. {
  509. LOGERROR("Cube texture face 0 must be loaded first");
  510. return false;
  511. }
  512. if (width != width_ || format != format_)
  513. {
  514. LOGERROR("Cube texture face does not match size or format of face 0");
  515. return false;
  516. }
  517. }
  518. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  519. {
  520. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  521. if (!needDecompress)
  522. {
  523. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  524. memoryUse += level.rows_ * level.rowSize_;
  525. }
  526. else
  527. {
  528. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  529. level.Decompress(rgbaData);
  530. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  531. memoryUse += level.width_ * level.height_ * 4;
  532. delete[] rgbaData;
  533. }
  534. }
  535. }
  536. faceMemoryUse_[face] = memoryUse;
  537. unsigned totalMemoryUse = sizeof(TextureCube);
  538. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  539. totalMemoryUse += faceMemoryUse_[i];
  540. SetMemoryUse(totalMemoryUse);
  541. return true;
  542. }
  543. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  544. {
  545. if (!object_)
  546. {
  547. LOGERROR("No texture created, can not get data");
  548. return false;
  549. }
  550. if (!dest)
  551. {
  552. LOGERROR("Null destination for getting data");
  553. return false;
  554. }
  555. if (level >= levels_)
  556. {
  557. LOGERROR("Illegal mip level for getting data");
  558. return false;
  559. }
  560. if (graphics_->IsDeviceLost())
  561. {
  562. LOGWARNING("Getting texture data while device is lost");
  563. return false;
  564. }
  565. int levelWidth = GetLevelWidth(level);
  566. int levelHeight = GetLevelHeight(level);
  567. D3DLOCKED_RECT d3dLockedRect;
  568. RECT d3dRect;
  569. d3dRect.left = 0;
  570. d3dRect.top = 0;
  571. d3dRect.right = levelWidth;
  572. d3dRect.bottom = levelHeight;
  573. if (FAILED(((IDirect3DCubeTexture9*)object_)->LockRect((D3DCUBEMAP_FACES)face, level, &d3dLockedRect, &d3dRect, D3DLOCK_READONLY)))
  574. {
  575. LOGERROR("Could not lock texture");
  576. return false;
  577. }
  578. int height = levelHeight;
  579. if (IsCompressed())
  580. height = (height + 3) >> 2;
  581. unsigned char* destPtr = (unsigned char*)dest;
  582. unsigned rowSize = GetRowDataSize(levelWidth);
  583. // GetRowDataSize() returns CPU-side (destination) data size, so need to convert for X8R8G8B8
  584. if (format_ == D3DFMT_X8R8G8B8)
  585. rowSize = rowSize / 3 * 4;
  586. // Perform conversion to RGB / RGBA as necessary
  587. switch (format_)
  588. {
  589. default:
  590. for (int i = 0; i < height; ++i)
  591. {
  592. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  593. memcpy(destPtr, src, rowSize);
  594. destPtr += rowSize;
  595. }
  596. break;
  597. case D3DFMT_X8R8G8B8:
  598. for (int i = 0; i < height; ++i)
  599. {
  600. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  601. for (int j = 0; j < levelWidth; ++j)
  602. {
  603. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; ++src;
  604. destPtr += 3;
  605. }
  606. }
  607. break;
  608. case D3DFMT_A8R8G8B8:
  609. for (int i = 0; i < height; ++i)
  610. {
  611. unsigned char* src = (unsigned char*)d3dLockedRect.pBits + i * d3dLockedRect.Pitch;
  612. for (int j = 0; j < levelWidth; ++j)
  613. {
  614. destPtr[2] = *src++; destPtr[1] = *src++; destPtr[0] = *src++; destPtr[3] = *src++;
  615. destPtr += 4;
  616. }
  617. }
  618. break;
  619. }
  620. ((IDirect3DCubeTexture9*)object_)->UnlockRect((D3DCUBEMAP_FACES)face, level);
  621. return true;
  622. }
  623. bool TextureCube::Create()
  624. {
  625. Release();
  626. if (!graphics_ || !width_ || !height_)
  627. return false;
  628. if (graphics_->IsDeviceLost())
  629. {
  630. LOGWARNING("Texture creation while device is lost");
  631. return true;
  632. }
  633. IDirect3DDevice9* device = graphics_->GetImpl()->GetDevice();
  634. if (!device || FAILED(device->CreateCubeTexture(
  635. width_,
  636. requestedLevels_,
  637. usage_,
  638. (D3DFORMAT)format_,
  639. (D3DPOOL)pool_,
  640. (IDirect3DCubeTexture9**)&object_,
  641. 0)))
  642. {
  643. LOGERROR("Could not create cube texture");
  644. return false;
  645. }
  646. levels_ = ((IDirect3DCubeTexture9*)object_)->GetLevelCount();
  647. if (usage_ & D3DUSAGE_RENDERTARGET)
  648. {
  649. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  650. {
  651. ((IDirect3DCubeTexture9*)object_)->GetCubeMapSurface((D3DCUBEMAP_FACES)i, 0,
  652. (IDirect3DSurface9**)&renderSurfaces_[i]->surface_);
  653. }
  654. }
  655. return true;
  656. }
  657. void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  658. {
  659. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  660. {
  661. if (renderSurfaces_[i] && renderSurfaces_[i]->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  662. renderSurfaces_[i]->QueueUpdate();
  663. }
  664. }
  665. }