2
0

OGLTexture2D.cpp 14 KB

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