OGLTextureCube.cpp 22 KB

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