OGLTextureCube.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. //
  2. // Copyright (c) 2008-2016 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. void TextureCube::OnDeviceLost()
  41. {
  42. GPUObject::OnDeviceLost();
  43. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  44. {
  45. if (renderSurfaces_[i])
  46. renderSurfaces_[i]->OnDeviceLost();
  47. }
  48. }
  49. void TextureCube::OnDeviceReset()
  50. {
  51. if (!object_.name_ || dataPending_)
  52. {
  53. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  54. ResourceCache* cache = GetSubsystem<ResourceCache>();
  55. if (cache->Exists(GetName()))
  56. dataLost_ = !cache->ReloadResource(this);
  57. if (!object_.name_)
  58. {
  59. Create();
  60. dataLost_ = true;
  61. }
  62. }
  63. dataPending_ = false;
  64. }
  65. void TextureCube::Release()
  66. {
  67. if (object_.name_)
  68. {
  69. if (!graphics_)
  70. return;
  71. if (!graphics_->IsDeviceLost())
  72. {
  73. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  74. {
  75. if (graphics_->GetTexture(i) == this)
  76. graphics_->SetTexture(i, 0);
  77. }
  78. glDeleteTextures(1, &object_.name_);
  79. }
  80. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  81. {
  82. if (renderSurfaces_[i])
  83. renderSurfaces_[i]->Release();
  84. }
  85. object_.name_ = 0;
  86. }
  87. }
  88. bool TextureCube::SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data)
  89. {
  90. ATOMIC_PROFILE(SetTextureData);
  91. if (!object_.name_ || !graphics_)
  92. {
  93. ATOMIC_LOGERROR("No texture created, can not set data");
  94. return false;
  95. }
  96. if (!data)
  97. {
  98. ATOMIC_LOGERROR("Null source for setting data");
  99. return false;
  100. }
  101. if (level >= levels_)
  102. {
  103. ATOMIC_LOGERROR("Illegal mip level for setting data");
  104. return false;
  105. }
  106. if (graphics_->IsDeviceLost())
  107. {
  108. ATOMIC_LOGWARNING("Texture data assignment while device is lost");
  109. dataPending_ = true;
  110. return true;
  111. }
  112. if (IsCompressed())
  113. {
  114. x &= ~3;
  115. y &= ~3;
  116. }
  117. int levelWidth = GetLevelWidth(level);
  118. int levelHeight = GetLevelHeight(level);
  119. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  120. {
  121. ATOMIC_LOGERROR("Illegal dimensions for setting data");
  122. return false;
  123. }
  124. graphics_->SetTextureForUpdate(this);
  125. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  126. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  127. if (!IsCompressed())
  128. {
  129. if (wholeLevel)
  130. glTexImage2D((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, format, width, height, 0, GetExternalFormat(format_),
  131. GetDataType(format_), data);
  132. else
  133. glTexSubImage2D((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, x, y, width, height, GetExternalFormat(format_),
  134. GetDataType(format_), data);
  135. }
  136. else
  137. {
  138. if (wholeLevel)
  139. glCompressedTexImage2D((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, format, width, height, 0,
  140. GetDataSize(width, height), data);
  141. else
  142. glCompressedTexSubImage2D((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, x, y, width, height, format,
  143. GetDataSize(width, height), data);
  144. }
  145. graphics_->SetTexture(0, 0);
  146. return true;
  147. }
  148. bool TextureCube::SetData(CubeMapFace face, Deserializer& source)
  149. {
  150. SharedPtr<Image> image(new Image(context_));
  151. if (!image->Load(source))
  152. return false;
  153. return SetData(face, image);
  154. }
  155. bool TextureCube::SetData(CubeMapFace face, Image* image, bool useAlpha)
  156. {
  157. if (!image)
  158. {
  159. ATOMIC_LOGERROR("Null image, can not set face data");
  160. return false;
  161. }
  162. // Use a shared ptr for managing the temporary mip images created during this function
  163. SharedPtr<Image> mipImage;
  164. unsigned memoryUse = 0;
  165. int quality = QUALITY_HIGH;
  166. Renderer* renderer = GetSubsystem<Renderer>();
  167. if (renderer)
  168. quality = renderer->GetTextureQuality();
  169. if (!image->IsCompressed())
  170. {
  171. // Convert unsuitable formats to RGBA
  172. unsigned components = image->GetComponents();
  173. if (Graphics::GetGL3Support() && ((components == 1 && !useAlpha) || components == 2))
  174. {
  175. mipImage = image->ConvertToRGBA(); image = mipImage;
  176. if (!image)
  177. return false;
  178. components = image->GetComponents();
  179. }
  180. unsigned char* levelData = image->GetData();
  181. int levelWidth = image->GetWidth();
  182. int levelHeight = image->GetHeight();
  183. unsigned format = 0;
  184. if (levelWidth != levelHeight)
  185. {
  186. ATOMIC_LOGERROR("Cube texture width not equal to height");
  187. return false;
  188. }
  189. // Discard unnecessary mip levels
  190. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  191. {
  192. mipImage = image->GetNextLevel(); image = mipImage;
  193. levelData = image->GetData();
  194. levelWidth = image->GetWidth();
  195. levelHeight = image->GetHeight();
  196. }
  197. switch (components)
  198. {
  199. case 1:
  200. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  201. break;
  202. case 2:
  203. format = Graphics::GetLuminanceAlphaFormat();
  204. break;
  205. case 3:
  206. format = Graphics::GetRGBFormat();
  207. break;
  208. case 4:
  209. format = Graphics::GetRGBAFormat();
  210. break;
  211. default:
  212. assert(false); // Should not reach here
  213. break;
  214. }
  215. // Create the texture when face 0 is being loaded, check that rest of the faces are same size & format
  216. if (!face)
  217. {
  218. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  219. if (IsCompressed() && requestedLevels_ > 1)
  220. requestedLevels_ = 0;
  221. SetSize(levelWidth, format);
  222. }
  223. else
  224. {
  225. if (!object_.name_)
  226. {
  227. ATOMIC_LOGERROR("Cube texture face 0 must be loaded first");
  228. return false;
  229. }
  230. if (levelWidth != width_ || format != format_)
  231. {
  232. ATOMIC_LOGERROR("Cube texture face does not match size or format of face 0");
  233. return false;
  234. }
  235. }
  236. for (unsigned i = 0; i < levels_; ++i)
  237. {
  238. SetData(face, i, 0, 0, levelWidth, levelHeight, levelData);
  239. memoryUse += levelWidth * levelHeight * components;
  240. if (i < levels_ - 1)
  241. {
  242. mipImage = image->GetNextLevel(); image = mipImage;
  243. levelData = image->GetData();
  244. levelWidth = image->GetWidth();
  245. levelHeight = image->GetHeight();
  246. }
  247. }
  248. }
  249. else
  250. {
  251. int width = image->GetWidth();
  252. int height = image->GetHeight();
  253. unsigned levels = image->GetNumCompressedLevels();
  254. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  255. bool needDecompress = false;
  256. if (width != height)
  257. {
  258. ATOMIC_LOGERROR("Cube texture width not equal to height");
  259. return false;
  260. }
  261. if (!format)
  262. {
  263. format = Graphics::GetRGBAFormat();
  264. needDecompress = true;
  265. }
  266. unsigned mipsToSkip = mipsToSkip_[quality];
  267. if (mipsToSkip >= levels)
  268. mipsToSkip = levels - 1;
  269. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  270. --mipsToSkip;
  271. width /= (1 << mipsToSkip);
  272. height /= (1 << mipsToSkip);
  273. // Create the texture when face 0 is being loaded, assume rest of the faces are same size & format
  274. if (!face)
  275. {
  276. SetNumLevels(Max((levels - mipsToSkip), 1U));
  277. SetSize(width, format);
  278. }
  279. else
  280. {
  281. if (!object_.name_)
  282. {
  283. ATOMIC_LOGERROR("Cube texture face 0 must be loaded first");
  284. return false;
  285. }
  286. if (width != width_ || format != format_)
  287. {
  288. ATOMIC_LOGERROR("Cube texture face does not match size or format of face 0");
  289. return false;
  290. }
  291. }
  292. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  293. {
  294. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  295. if (!needDecompress)
  296. {
  297. SetData(face, i, 0, 0, level.width_, level.height_, level.data_);
  298. memoryUse += level.rows_ * level.rowSize_;
  299. }
  300. else
  301. {
  302. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  303. level.Decompress(rgbaData);
  304. SetData(face, i, 0, 0, level.width_, level.height_, rgbaData);
  305. memoryUse += level.width_ * level.height_ * 4;
  306. delete[] rgbaData;
  307. }
  308. }
  309. }
  310. faceMemoryUse_[face] = memoryUse;
  311. unsigned totalMemoryUse = sizeof(TextureCube);
  312. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  313. totalMemoryUse += faceMemoryUse_[i];
  314. SetMemoryUse(totalMemoryUse);
  315. return true;
  316. }
  317. bool TextureCube::GetData(CubeMapFace face, unsigned level, void* dest) const
  318. {
  319. if (!object_.name_ || !graphics_)
  320. {
  321. ATOMIC_LOGERROR("No texture created, can not get data");
  322. return false;
  323. }
  324. #ifndef GL_ES_VERSION_2_0
  325. if (!dest)
  326. {
  327. ATOMIC_LOGERROR("Null destination for getting data");
  328. return false;
  329. }
  330. if (level >= levels_)
  331. {
  332. ATOMIC_LOGERROR("Illegal mip level for getting data");
  333. return false;
  334. }
  335. if (graphics_->IsDeviceLost())
  336. {
  337. ATOMIC_LOGWARNING("Getting texture data while device is lost");
  338. return false;
  339. }
  340. graphics_->SetTextureForUpdate(const_cast<TextureCube*>(this));
  341. if (!IsCompressed())
  342. glGetTexImage((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, GetExternalFormat(format_), GetDataType(format_), dest);
  343. else
  344. glGetCompressedTexImage((GLenum)(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face), level, dest);
  345. graphics_->SetTexture(0, 0);
  346. return true;
  347. #else
  348. // Special case on GLES: if the texture is a rendertarget, can make it current and use glReadPixels()
  349. if (usage_ == TEXTURE_RENDERTARGET)
  350. {
  351. graphics_->SetRenderTarget(0, renderSurfaces_[face]);
  352. // Ensure the FBO is current; this viewport is actually never rendered to
  353. graphics_->SetViewport(IntRect(0, 0, width_, height_));
  354. glReadPixels(0, 0, width_, height_, GetExternalFormat(format_), GetDataType(format_), dest);
  355. return true;
  356. }
  357. ATOMIC_LOGERROR("Getting texture data not supported");
  358. return false;
  359. #endif
  360. }
  361. bool TextureCube::Create()
  362. {
  363. Release();
  364. if (!graphics_ || !width_ || !height_)
  365. return false;
  366. if (graphics_->IsDeviceLost())
  367. {
  368. ATOMIC_LOGWARNING("Texture creation while device is lost");
  369. return true;
  370. }
  371. glGenTextures(1, &object_.name_);
  372. // Ensure that our texture is bound to OpenGL texture unit 0
  373. graphics_->SetTextureForUpdate(this);
  374. // If not compressed, create the initial level 0 texture with null data
  375. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  376. unsigned externalFormat = GetExternalFormat(format_);
  377. unsigned dataType = GetDataType(format_);
  378. bool success = true;
  379. if (!IsCompressed())
  380. {
  381. glGetError();
  382. for (unsigned i = 0; i < MAX_CUBEMAP_FACES; ++i)
  383. {
  384. glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, format, width_, height_, 0, externalFormat, dataType, 0);
  385. if (glGetError())
  386. success = false;
  387. }
  388. }
  389. if (!success)
  390. ATOMIC_LOGERROR("Failed to create texture");
  391. // Set mipmapping
  392. levels_ = CheckMaxLevels(width_, height_, requestedLevels_);
  393. #ifndef GL_ES_VERSION_2_0
  394. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  395. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  396. #endif
  397. // Set initial parameters, then unbind the texture
  398. UpdateParameters();
  399. graphics_->SetTexture(0, 0);
  400. return success;
  401. }
  402. }