OGLTextureCube.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. {
  54. target_ = GL_TEXTURE_CUBE_MAP;
  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. GPUObject::OnDeviceLost();
  206. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  207. {
  208. if (renderSurfaces_[i])
  209. renderSurfaces_[i]->OnDeviceLost();
  210. }
  211. }
  212. void TextureCube::OnDeviceReset()
  213. {
  214. if (!object_ || dataPending_)
  215. {
  216. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  217. ResourceCache* cache = GetSubsystem<ResourceCache>();
  218. if (cache->Exists(GetName()))
  219. dataLost_ = !cache->ReloadResource(this);
  220. if (!object_)
  221. {
  222. Create();
  223. dataLost_ = true;
  224. }
  225. }
  226. dataPending_ = false;
  227. }
  228. void TextureCube::Release()
  229. {
  230. if (object_)
  231. {
  232. if (!graphics_)
  233. return;
  234. if (!graphics_->IsDeviceLost())
  235. {
  236. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  237. {
  238. if (graphics_->GetTexture(i) == this)
  239. graphics_->SetTexture(i, 0);
  240. }
  241. glDeleteTextures(1, &object_);
  242. }
  243. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  244. {
  245. if (renderSurfaces_[i])
  246. renderSurfaces_[i]->Release();
  247. }
  248. object_ = 0;
  249. }
  250. }
  251. bool TextureCube::SetSize(int size, unsigned format, TextureUsage usage)
  252. {
  253. if (size <= 0)
  254. {
  255. LOGERROR("Zero or negative cube texture size");
  256. return false;
  257. }
  258. if (usage == TEXTURE_DEPTHSTENCIL)
  259. {
  260. LOGERROR("Depth-stencil usage not supported for cube maps");
  261. return false;
  262. }
  263. // Delete the old rendersurfaces if any
  264. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  265. {
  266. renderSurfaces_[i].Reset();
  267. faceMemoryUse_[i] = 0;
  268. }
  269. usage_ = usage;
  270. if (usage == TEXTURE_RENDERTARGET)
  271. {
  272. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  273. {
  274. renderSurfaces_[i] = new RenderSurface(this);
  275. renderSurfaces_[i]->SetTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i);
  276. }
  277. // Nearest filtering and mipmaps disabled by default
  278. filterMode_ = FILTER_NEAREST;
  279. requestedLevels_ = 1;
  280. }
  281. if (usage == TEXTURE_RENDERTARGET)
  282. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(TextureCube, HandleRenderSurfaceUpdate));
  283. else
  284. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  285. width_ = size;
  286. height_ = size;
  287. format_ = format;
  288. return Create();
  289. }
  290. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  291. {
  292. PROFILE(SetTextureData);
  293. if (!object_ || !graphics_)
  294. {
  295. LOGERROR("No texture created, can not set data");
  296. return false;
  297. }
  298. if (!data)
  299. {
  300. LOGERROR("Null source for setting data");
  301. return false;
  302. }
  303. if (level >= levels_)
  304. {
  305. LOGERROR("Illegal mip level for setting data");
  306. return false;
  307. }
  308. if (graphics_->IsDeviceLost())
  309. {
  310. LOGWARNING("Texture data assignment while device is lost");
  311. dataPending_ = true;
  312. return true;
  313. }
  314. if (IsCompressed())
  315. {
  316. x &= ~3;
  317. y &= ~3;
  318. }
  319. int levelWidth = GetLevelWidth(level);
  320. int levelHeight = GetLevelHeight(level);
  321. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  322. {
  323. LOGERROR("Illegal dimensions for setting data");
  324. return false;
  325. }
  326. graphics_->SetTextureForUpdate(this);
  327. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  328. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  329. if (!IsCompressed())
  330. {
  331. if (wholeLevel)
  332. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format, width, height, 0, GetExternalFormat(format_),
  333. GetDataType(format_), data);
  334. else
  335. glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, GetExternalFormat(format_),
  336. GetDataType(format_), data);
  337. }
  338. else
  339. {
  340. if (wholeLevel)
  341. glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, format, width, height, 0,
  342. GetDataSize(width, height), data);
  343. else
  344. glCompressedTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, x, y, width, height, format,
  345. GetDataSize(width, height), data);
  346. }
  347. graphics_->SetTexture(0, 0);
  348. return true;
  349. }
  350. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  351. {
  352. SharedPtr<Image> image(new Image(context_));
  353. if (!image->Load(source))
  354. return false;
  355. return SetData(face, image);
  356. }
  357. bool TextureCube::SetData(CubeMapFace face, SharedPtr<Image> image, bool useAlpha)
  358. {
  359. if (!image)
  360. {
  361. LOGERROR("Null image, can not set face data");
  362. return false;
  363. }
  364. unsigned memoryUse = 0;
  365. int quality = QUALITY_HIGH;
  366. Renderer* renderer = GetSubsystem<Renderer>();
  367. if (renderer)
  368. quality = renderer->GetTextureQuality();
  369. if (!image->IsCompressed())
  370. {
  371. // Convert unsuitable formats to RGBA
  372. unsigned components = image->GetComponents();
  373. if (Graphics::GetGL3Support() && ((components == 1 && !useAlpha) || components == 2))
  374. {
  375. image = image->ConvertToRGBA();
  376. if (!image)
  377. return false;
  378. components = image->GetComponents();
  379. }
  380. unsigned char* levelData = image->GetData();
  381. int levelWidth = image->GetWidth();
  382. int levelHeight = image->GetHeight();
  383. unsigned format = 0;
  384. if (levelWidth != levelHeight)
  385. {
  386. LOGERROR("Cube texture width not equal to height");
  387. return false;
  388. }
  389. // Discard unnecessary mip levels
  390. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  391. {
  392. image = image->GetNextLevel();
  393. levelData = image->GetData();
  394. levelWidth = image->GetWidth();
  395. levelHeight = image->GetHeight();
  396. }
  397. switch (components)
  398. {
  399. case 1:
  400. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  401. break;
  402. case 2:
  403. format = Graphics::GetLuminanceAlphaFormat();
  404. break;
  405. case 3:
  406. format = Graphics::GetRGBFormat();
  407. break;
  408. case 4:
  409. format = Graphics::GetRGBAFormat();
  410. break;
  411. }
  412. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  413. if (!face)
  414. {
  415. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  416. if (IsCompressed() && requestedLevels_ > 1)
  417. requestedLevels_ = 0;
  418. SetSize(levelWidth, format);
  419. }
  420. else
  421. {
  422. if (!object_)
  423. {
  424. LOGERROR("Cube texture face 0 must be loaded first");
  425. return false;
  426. }
  427. if (levelWidth != width_ || format != format_)
  428. {
  429. LOGERROR("Cube texture face does not match size or format of face 0");
  430. return false;
  431. }
  432. }
  433. for (unsigned i = 0; i < levels_; ++i)
  434. {
  435. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  436. memoryUse += levelWidth * levelHeight * components;
  437. if (i < levels_ - 1)
  438. {
  439. image = image->GetNextLevel();
  440. levelData = image->GetData();
  441. levelWidth = image->GetWidth();
  442. levelHeight = image->GetHeight();
  443. }
  444. }
  445. }
  446. else
  447. {
  448. int width = image->GetWidth();
  449. int height = image->GetHeight();
  450. unsigned levels = image->GetNumCompressedLevels();
  451. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  452. bool needDecompress = false;
  453. if (width != height)
  454. {
  455. LOGERROR("Cube texture width not equal to height");
  456. return false;
  457. }
  458. if (!format)
  459. {
  460. format = Graphics::GetRGBAFormat();
  461. needDecompress = true;
  462. }
  463. unsigned mipsToSkip = mipsToSkip_[quality];
  464. if (mipsToSkip >= levels)
  465. mipsToSkip = levels - 1;
  466. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  467. --mipsToSkip;
  468. width /= (1 << mipsToSkip);
  469. height /= (1 << mipsToSkip);
  470. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  471. if (!face)
  472. {
  473. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  474. SetSize(width, format);
  475. }
  476. else
  477. {
  478. if (!object_)
  479. {
  480. LOGERROR("Cube texture face 0 must be loaded first");
  481. return false;
  482. }
  483. if (width != width_ || format != format_)
  484. {
  485. LOGERROR("Cube texture face does not match size or format of face 0");
  486. return false;
  487. }
  488. }
  489. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  490. {
  491. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  492. if (!needDecompress)
  493. {
  494. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  495. memoryUse += level.rows_ * level.rowSize_;
  496. }
  497. else
  498. {
  499. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  500. level.Decompress(rgbaData);
  501. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  502. memoryUse += level.width_ * level.height_ * 4;
  503. delete[] rgbaData;
  504. }
  505. }
  506. }
  507. faceMemoryUse_[face] = memoryUse;
  508. unsigned totalMemoryUse = sizeof(TextureCube);
  509. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  510. totalMemoryUse += faceMemoryUse_[i];
  511. SetMemoryUse(totalMemoryUse);
  512. return true;
  513. }
  514. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  515. {
  516. #ifndef GL_ES_VERSION_2_0
  517. if (!object_ || !graphics_)
  518. {
  519. LOGERROR("No texture created, can not get data");
  520. return false;
  521. }
  522. if (!dest)
  523. {
  524. LOGERROR("Null destination for getting data");
  525. return false;
  526. }
  527. if (level >= levels_)
  528. {
  529. LOGERROR("Illegal mip level for getting data");
  530. return false;
  531. }
  532. if (graphics_->IsDeviceLost())
  533. {
  534. LOGWARNING("Getting texture data while device is lost");
  535. return false;
  536. }
  537. graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));
  538. if (!IsCompressed())
  539. glGetTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, GetExternalFormat(format_), GetDataType(format_), dest);
  540. else
  541. glGetCompressedTexImage(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, dest);
  542. graphics_->SetTexture(0, 0);
  543. return true;
  544. #else
  545. LOGERROR("Getting texture data not supported");
  546. return false;
  547. #endif
  548. }
  549. bool TextureCube::Create()
  550. {
  551. Release();
  552. if (!graphics_ || !width_ || !height_)
  553. return false;
  554. if (graphics_->IsDeviceLost())
  555. {
  556. LOGWARNING("Texture creation while device is lost");
  557. return true;
  558. }
  559. glGenTextures(1, &object_);
  560. // Ensure that our texture is bound to OpenGL texture unit 0
  561. graphics_->SetTextureForUpdate(this);
  562. // If not compressed, create the initial level 0 texture with null data
  563. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  564. unsigned externalFormat = GetExternalFormat(format_);
  565. unsigned dataType = GetDataType(format_);
  566. bool success = true;
  567. if (!IsCompressed())
  568. {
  569. glGetError();
  570. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  571. {
  572. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width_, height_, 0, externalFormat, dataType, 0);
  573. if (glGetError())
  574. success = false;
  575. }
  576. }
  577. if (!success)
  578. LOGERROR("Failed to create texture");
  579. // Set mipmapping
  580. levels_ = requestedLevels_;
  581. if (!levels_)
  582. {
  583. unsigned maxSize = Max((int)width_, (int)height_);
  584. while (maxSize)
  585. {
  586. maxSize >>= 1;
  587. ++levels_;
  588. }
  589. }
  590. #ifndef GL_ES_VERSION_2_0
  591. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  592. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  593. #endif
  594. // Set initial parameters, then unbind the texture
  595. UpdateParameters();
  596. graphics_->SetTexture(0, 0);
  597. return success;
  598. }
  599. void TextureCube::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  600. {
  601. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  602. {
  603. if (renderSurfaces_[i] && renderSurfaces_[i]->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  604. renderSurfaces_[i]->QueueUpdate();
  605. }
  606. }
  607. }