OGLTextureCube.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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 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>(
  51. src->GetSubimage(IntRect(tileX * tileWidth, tileY * tileHeight, (tileX + 1) * tileWidth, (tileY + 1) * tileHeight)));
  52. }
  53. TextureCube::TextureCube(Context* context) :
  54. Texture(context)
  55. {
  56. target_ = GL_TEXTURE_CUBE_MAP;
  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. SharedPtr<Image> image = cache->GetTempResource<Image>(name);
  105. if (!image)
  106. return false;
  107. int faceWidth, faceHeight;
  108. loadImages_.Resize(MAX_CUBEMAP_FACES);
  109. if (image->IsCubemap())
  110. {
  111. loadImages_[FACE_POSITIVE_X] = image;
  112. loadImages_[FACE_NEGATIVE_X] = loadImages_[FACE_POSITIVE_X]->GetNextSibling();
  113. loadImages_[FACE_POSITIVE_Y] = loadImages_[FACE_NEGATIVE_X]->GetNextSibling();
  114. loadImages_[FACE_NEGATIVE_Y] = loadImages_[FACE_POSITIVE_Y]->GetNextSibling();
  115. loadImages_[FACE_POSITIVE_Z] = loadImages_[FACE_NEGATIVE_Y]->GetNextSibling();
  116. loadImages_[FACE_NEGATIVE_Z] = loadImages_[FACE_POSITIVE_Z]->GetNextSibling();
  117. }
  118. else
  119. {
  120. CubeMapLayout layout =
  121. (CubeMapLayout)GetStringListIndex(imageElem.GetAttribute("layout").CString(), cubeMapLayoutNames, CML_HORIZONTAL);
  122. switch (layout)
  123. {
  124. case CML_HORIZONTAL:
  125. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  126. faceHeight = image->GetHeight();
  127. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  128. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  129. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  130. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 3, 0, faceWidth, faceHeight);
  131. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 4, 0, faceWidth, faceHeight);
  132. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 5, 0, faceWidth, faceHeight);
  133. break;
  134. case CML_HORIZONTALNVIDIA:
  135. faceWidth = image->GetWidth() / MAX_CUBEMAP_FACES;
  136. faceHeight = image->GetHeight();
  137. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  138. loadImages_[i] = GetTileImage(image, i, 0, faceWidth, faceHeight);
  139. break;
  140. case CML_HORIZONTALCROSS:
  141. faceWidth = image->GetWidth() / 4;
  142. faceHeight = image->GetHeight() / 3;
  143. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  144. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  145. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  146. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  147. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 3, 1, faceWidth, faceHeight);
  148. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  149. break;
  150. case CML_VERTICALCROSS:
  151. faceWidth = image->GetWidth() / 3;
  152. faceHeight = image->GetHeight() / 4;
  153. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  154. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  155. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  156. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  157. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 1, 2, faceWidth, faceHeight);
  158. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 3, faceWidth, faceHeight);
  159. if (loadImages_[FACE_NEGATIVE_Z])
  160. {
  161. loadImages_[FACE_NEGATIVE_Z]->FlipVertical();
  162. loadImages_[FACE_NEGATIVE_Z]->FlipHorizontal();
  163. }
  164. break;
  165. case CML_BLENDER:
  166. faceWidth = image->GetWidth() / 3;
  167. faceHeight = image->GetHeight() / 2;
  168. loadImages_[FACE_NEGATIVE_X] = GetTileImage(image, 0, 0, faceWidth, faceHeight);
  169. loadImages_[FACE_NEGATIVE_Z] = GetTileImage(image, 1, 0, faceWidth, faceHeight);
  170. loadImages_[FACE_POSITIVE_X] = GetTileImage(image, 2, 0, faceWidth, faceHeight);
  171. loadImages_[FACE_NEGATIVE_Y] = GetTileImage(image, 0, 1, faceWidth, faceHeight);
  172. loadImages_[FACE_POSITIVE_Y] = GetTileImage(image, 1, 1, faceWidth, faceHeight);
  173. loadImages_[FACE_POSITIVE_Z] = GetTileImage(image, 2, 1, faceWidth, faceHeight);
  174. break;
  175. }
  176. }
  177. }
  178. // Face per image
  179. else
  180. {
  181. XMLElement faceElem = textureElem.GetChild("face");
  182. while (faceElem)
  183. {
  184. String name = faceElem.GetAttribute("name");
  185. // If path is empty, add the XML file path
  186. if (GetPath(name).Empty())
  187. name = texPath + name;
  188. loadImages_.Push(cache->GetTempResource<Image>(name));
  189. cache->StoreResourceDependency(this, name);
  190. faceElem = faceElem.GetNext("face");
  191. }
  192. }
  193. // Precalculate mip levels if async loading
  194. if (GetAsyncLoadState() == ASYNC_LOADING)
  195. {
  196. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  197. {
  198. if (loadImages_[i])
  199. loadImages_[i]->PrecalculateLevels();
  200. }
  201. }
  202. return true;
  203. }
  204. bool TextureCube::EndLoad()
  205. {
  206. // In headless mode, do not actually load the texture, just return success
  207. if (!graphics_ || graphics_->IsDeviceLost())
  208. return true;
  209. // If over the texture budget, see if materials can be freed to allow textures to be freed
  210. CheckTextureBudget(GetTypeStatic());
  211. SetParameters(loadParameters_);
  212. for (unsigned i = 0; i < loadImages_.Size() && i < MAX_CUBEMAP_FACES; ++i)
  213. SetData((CubeMapFace)i, loadImages_[i]);
  214. loadImages_.Clear();
  215. loadParameters_.Reset();
  216. return true;
  217. }
  218. void TextureCube::OnDeviceLost()
  219. {
  220. GPUObject::OnDeviceLost();
  221. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  222. {
  223. if (renderSurfaces_[i])
  224. renderSurfaces_[i]->OnDeviceLost();
  225. }
  226. }
  227. void TextureCube::OnDeviceReset()
  228. {
  229. if (!object_ || dataPending_)
  230. {
  231. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  232. ResourceCache* cache = GetSubsystem<ResourceCache>();
  233. if (cache->Exists(GetName()))
  234. dataLost_ = !cache->ReloadResource(this);
  235. if (!object_)
  236. {
  237. Create();
  238. dataLost_ = true;
  239. }
  240. }
  241. dataPending_ = false;
  242. }
  243. void TextureCube::Release()
  244. {
  245. if (object_)
  246. {
  247. if (!graphics_)
  248. return;
  249. if (!graphics_->IsDeviceLost())
  250. {
  251. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  252. {
  253. if (graphics_->GetTexture(i) == this)
  254. graphics_->SetTexture(i, 0);
  255. }
  256. glDeleteTextures(1, &object_);
  257. }
  258. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  259. {
  260. if (renderSurfaces_[i])
  261. renderSurfaces_[i]->Release();
  262. }
  263. object_ = 0;
  264. }
  265. }
  266. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  267. {
  268. if (size <= 0)
  269. {
  270. LOGERROR("Zero or negative cube texture size");
  271. return false;
  272. }
  273. if (usage == TEXTURE_DEPTHSTENCIL)
  274. {
  275. LOGERROR("Depth-stencil usage not supported for cube maps");
  276. return false;
  277. }
  278. // Delete the old rendersurfaces if any
  279. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  280. {
  281. renderSurfaces_[i].Reset();
  282. faceMemoryUse_[i] = 0;
  283. }
  284. usage_ = usage;
  285. if (usage == TEXTURE_RENDERTARGET)
  286. {
  287. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  288. {
  289. renderSurfaces_[i] = new RenderSurface(this);
  290. renderSurfaces_[i]->SetTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
  291. }
  292. // Nearest filtering and mipmaps disabled by default
  293. filterMode_ = FILTER_NEAREST;
  294. requestedLevels_ = 1;
  295. }
  296. if (usage == TEXTURE_RENDERTARGET)
  297. SubscribeToEvent(E_RENDERSURFACEUPDATE, URHO3D_HANDLER(TextureCube, HandleRenderSurfaceUpdate));
  298. else
  299. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  300. width_ = size;
  301. height_ = size;
  302. format_ = format;
  303. return Create();
  304. }
  305. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  306. {
  307. PROFILE(SetTextureData);
  308. if (!object_ || !graphics_)
  309. {
  310. LOGERROR("No texture created, can not set data");
  311. return false;
  312. }
  313. if (!data)
  314. {
  315. LOGERROR("Null source for setting data");
  316. return false;
  317. }
  318. if (level >= levels_)
  319. {
  320. LOGERROR("Illegal mip level for setting data");
  321. return false;
  322. }
  323. if (graphics_->IsDeviceLost())
  324. {
  325. LOGWARNING("Texture data assignment while device is lost");
  326. dataPending_ = true;
  327. return true;
  328. }
  329. if (IsCompressed())
  330. {
  331. x &= ~3;
  332. y &= ~3;
  333. }
  334. int levelWidth = GetLevelWidth(level);
  335. int levelHeight = GetLevelHeight(level);
  336. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  337. {
  338. LOGERROR("Illegal dimensions for setting data");
  339. return false;
  340. }
  341. graphics_->SetTextureForUpdate(this);
  342. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  343. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  344. if (!IsCompressed())
  345. {
  346. if (wholeLevel)
  347. glTexImage2D((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, format, width, height, 0, GetExternalFormat(format_),
  348. GetDataType(format_), data);
  349. else
  350. glTexSubImage2D((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, x, y, width, height, GetExternalFormat(format_),
  351. GetDataType(format_), data);
  352. }
  353. else
  354. {
  355. if (wholeLevel)
  356. glCompressedTexImage2D((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, format, width, height, 0,
  357. GetDataSize(width, height), data);
  358. else
  359. glCompressedTexSubImage2D((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, x, y, width, height, format,
  360. GetDataSize(width, height), data);
  361. }
  362. graphics_->SetTexture(0, 0);
  363. return true;
  364. }
  365. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  366. {
  367. SharedPtr<Image> image(new Image(context_));
  368. if (!image->Load(source))
  369. return false;
  370. return SetData(face, image);
  371. }
  372. bool TextureCube::SetData(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  373. {
  374. if (!image)
  375. {
  376. LOGERROR("Null image, can not set face data");
  377. return false;
  378. }
  379. unsigned memoryUse = 0;
  380. int quality = QUALITY_HIGH;
  381. Renderer* renderer = GetSubsystem<Renderer>();
  382. if (renderer)
  383. quality = renderer->GetTextureQuality();
  384. if (!image->IsCompressed())
  385. {
  386. // Convert unsuitable formats to RGBA
  387. unsigned components = image->GetComponents();
  388. if (Graphics::GetGL3Support() && ((components == 1 && !useAlpha) || components == 2))
  389. {
  390. image = image->ConvertToRGBA();
  391. if (!image)
  392. return false;
  393. components = image->GetComponents();
  394. }
  395. unsigned char* levelData = image->GetData();
  396. int levelWidth = image->GetWidth();
  397. int levelHeight = image->GetHeight();
  398. unsigned format = 0;
  399. if (levelWidth != levelHeight)
  400. {
  401. LOGERROR("Cube texture width not equal to height");
  402. return false;
  403. }
  404. // Discard unnecessary mip levels
  405. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  406. {
  407. image = image->GetNextLevel();
  408. levelData = image->GetData();
  409. levelWidth = image->GetWidth();
  410. levelHeight = image->GetHeight();
  411. }
  412. switch (components)
  413. {
  414. case 1:
  415. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  416. break;
  417. case 2:
  418. format = Graphics::GetLuminanceAlphaFormat();
  419. break;
  420. case 3:
  421. format = Graphics::GetRGBFormat();
  422. break;
  423. case 4:
  424. format = Graphics::GetRGBAFormat();
  425. break;
  426. default:
  427. assert(false); // Should not reach here
  428. break;
  429. }
  430. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  431. if (!face)
  432. {
  433. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  434. if (IsCompressed() && requestedLevels_ > 1)
  435. requestedLevels_ = 0;
  436. SetSize(levelWidth, format);
  437. }
  438. else
  439. {
  440. if (!object_)
  441. {
  442. LOGERROR("Cube texture face 0 must be loaded first");
  443. return false;
  444. }
  445. if (levelWidth != width_ || format != format_)
  446. {
  447. LOGERROR("Cube texture face does not match size or format of face 0");
  448. return false;
  449. }
  450. }
  451. for (unsigned i = 0; i < levels_; ++i)
  452. {
  453. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  454. memoryUse += levelWidth * levelHeight * components;
  455. if (i < levels_ - 1)
  456. {
  457. image = image->GetNextLevel();
  458. levelData = image->GetData();
  459. levelWidth = image->GetWidth();
  460. levelHeight = image->GetHeight();
  461. }
  462. }
  463. }
  464. else
  465. {
  466. int width = image->GetWidth();
  467. int height = image->GetHeight();
  468. unsigned levels = image->GetNumCompressedLevels();
  469. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  470. bool needDecompress = false;
  471. if (width != height)
  472. {
  473. LOGERROR("Cube texture width not equal to height");
  474. return false;
  475. }
  476. if (!format)
  477. {
  478. format = Graphics::GetRGBAFormat();
  479. needDecompress = true;
  480. }
  481. unsigned mipsToSkip = mipsToSkip_[quality];
  482. if (mipsToSkip >= levels)
  483. mipsToSkip = levels - 1;
  484. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  485. --mipsToSkip;
  486. width /= (1 << mipsToSkip);
  487. height /= (1 << mipsToSkip);
  488. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  489. if (!face)
  490. {
  491. SetNumLevels((unsigned)Max((int)(levels - mipsToSkip), 1));
  492. SetSize(width, format);
  493. }
  494. else
  495. {
  496. if (!object_)
  497. {
  498. LOGERROR("Cube texture face 0 must be loaded first");
  499. return false;
  500. }
  501. if (width != width_ || format != format_)
  502. {
  503. LOGERROR("Cube texture face does not match size or format of face 0");
  504. return false;
  505. }
  506. }
  507. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  508. {
  509. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  510. if (!needDecompress)
  511. {
  512. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  513. memoryUse += level.rows_ * level.rowSize_;
  514. }
  515. else
  516. {
  517. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  518. level.Decompress(rgbaData);
  519. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  520. memoryUse += level.width_ * level.height_ * 4;
  521. delete[] rgbaData;
  522. }
  523. }
  524. }
  525. faceMemoryUse_[face] = memoryUse;
  526. unsigned totalMemoryUse = sizeof(TextureCube);
  527. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  528. totalMemoryUse += faceMemoryUse_[i];
  529. SetMemoryUse(totalMemoryUse);
  530. return true;
  531. }
  532. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  533. {
  534. #ifndef GL_ES_VERSION_2_0
  535. if (!object_ || !graphics_)
  536. {
  537. LOGERROR("No texture created, can not get data");
  538. return false;
  539. }
  540. if (!dest)
  541. {
  542. LOGERROR("Null destination for getting data");
  543. return false;
  544. }
  545. if (level >= levels_)
  546. {
  547. LOGERROR("Illegal mip level for getting data");
  548. return false;
  549. }
  550. if (graphics_->IsDeviceLost())
  551. {
  552. LOGWARNING("Getting texture data while device is lost");
  553. return false;
  554. }
  555. graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));
  556. if (!IsCompressed())
  557. glGetTexImage((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, GetExternalFormat(format_), GetDataType(format_), dest);
  558. else
  559. glGetCompressedTexImage((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, dest);
  560. graphics_->SetTexture(0, 0);
  561. return true;
  562. #else
  563. LOGERROR("Getting texture data not supported");
  564. return false;
  565. #endif
  566. }
  567. bool TextureCube::Create()
  568. {
  569. Release();
  570. if (!graphics_ || !width_ || !height_)
  571. return false;
  572. if (graphics_->IsDeviceLost())
  573. {
  574. LOGWARNING("Texture creation while device is lost");
  575. return true;
  576. }
  577. glGenTextures(1, &object_);
  578. // Ensure that our texture is bound to OpenGL texture unit 0
  579. graphics_->SetTextureForUpdate(this);
  580. // If not compressed, create the initial level 0 texture with null data
  581. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  582. unsigned externalFormat = GetExternalFormat(format_);
  583. unsigned dataType = GetDataType(format_);
  584. bool success = true;
  585. if (!IsCompressed())
  586. {
  587. glGetError();
  588. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  589. {
  590. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width_, height_, 0, externalFormat, dataType, 0);
  591. if (glGetError())
  592. success = false;
  593. }
  594. }
  595. if (!success)
  596. LOGERROR("Failed to create texture");
  597. // Set mipmapping
  598. levels_ = requestedLevels_;
  599. if (!levels_)
  600. {
  601. unsigned maxSize = (unsigned)Max(width_, height_);
  602. while (maxSize)
  603. {
  604. maxSize >>= 1;
  605. ++levels_;
  606. }
  607. }
  608. #ifndef GL_ES_VERSION_2_0
  609. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  610. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  611. #endif
  612. // Set initial parameters, then unbind the texture
  613. UpdateParameters();
  614. graphics_->SetTexture(0, 0);
  615. return success;
  616. }
  617. void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  618. {
  619. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  620. {
  621. if (renderSurfaces_[i] && renderSurfaces_[i]->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  622. renderSurfaces_[i]->QueueUpdate();
  623. }
  624. }
  625. }