D3D9TextureCube.cpp 24 KB

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