OGLTextureCube.cpp 14 KB

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