D3D9TextureCube.cpp 24 KB

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