D3D9TextureCube.cpp 25 KB

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