OGLTexture2D.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 rightsR
  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/Texture2D.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. namespace Atomic
  36. {
  37. void Texture2D::OnDeviceLost()
  38. {
  39. GPUObject::OnDeviceLost();
  40. if (renderSurface_)
  41. renderSurface_->OnDeviceLost();
  42. }
  43. void Texture2D::OnDeviceReset()
  44. {
  45. if (!object_.name_ || dataPending_)
  46. {
  47. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  48. ResourceCache* cache = GetSubsystem<ResourceCache>();
  49. if (cache->Exists(GetName()))
  50. dataLost_ = !cache->ReloadResource(this);
  51. if (!object_.name_)
  52. {
  53. Create();
  54. dataLost_ = true;
  55. }
  56. }
  57. dataPending_ = false;
  58. }
  59. void Texture2D::Release()
  60. {
  61. if (object_.name_)
  62. {
  63. if (!graphics_)
  64. return;
  65. if (!graphics_->IsDeviceLost())
  66. {
  67. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  68. {
  69. if (graphics_->GetTexture(i) == this)
  70. graphics_->SetTexture(i, 0);
  71. }
  72. glDeleteTextures(1, &object_.name_);
  73. }
  74. if (renderSurface_)
  75. renderSurface_->Release();
  76. object_.name_ = 0;
  77. }
  78. else
  79. {
  80. if (renderSurface_)
  81. renderSurface_->Release();
  82. }
  83. }
  84. bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data)
  85. {
  86. ATOMIC_PROFILE(SetTextureData);
  87. if (!object_.name_ || !graphics_)
  88. {
  89. ATOMIC_LOGERROR("No texture created, can not set data");
  90. return false;
  91. }
  92. if (!data)
  93. {
  94. ATOMIC_LOGERROR("Null source for setting data");
  95. return false;
  96. }
  97. if (level >= levels_)
  98. {
  99. ATOMIC_LOGERROR("Illegal mip level for setting data");
  100. return false;
  101. }
  102. if (graphics_->IsDeviceLost())
  103. {
  104. ATOMIC_LOGWARNING("Texture data assignment while device is lost");
  105. dataPending_ = true;
  106. return true;
  107. }
  108. if (IsCompressed())
  109. {
  110. x &= ~3;
  111. y &= ~3;
  112. }
  113. int levelWidth = GetLevelWidth(level);
  114. int levelHeight = GetLevelHeight(level);
  115. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  116. {
  117. ATOMIC_LOGERROR("Illegal dimensions for setting data");
  118. return false;
  119. }
  120. graphics_->SetTextureForUpdate(this);
  121. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight;
  122. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  123. if (!IsCompressed())
  124. {
  125. if (wholeLevel)
  126. glTexImage2D(target_, level, format, width, height, 0, GetExternalFormat(format_), GetDataType(format_), data);
  127. else
  128. glTexSubImage2D(target_, level, x, y, width, height, GetExternalFormat(format_), GetDataType(format_), data);
  129. }
  130. else
  131. {
  132. if (wholeLevel)
  133. glCompressedTexImage2D(target_, level, format, width, height, 0, GetDataSize(width, height), data);
  134. else
  135. glCompressedTexSubImage2D(target_, level, x, y, width, height, format, GetDataSize(width, height), data);
  136. }
  137. graphics_->SetTexture(0, 0);
  138. return true;
  139. }
  140. bool Texture2D::SetData(Image* image, bool useAlpha)
  141. {
  142. if (!image)
  143. {
  144. ATOMIC_LOGERROR("Null image, can not set 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 = sizeof(Texture2D);
  150. int quality = QUALITY_HIGH;
  151. Renderer* 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. // Discard unnecessary mip levels
  170. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  171. {
  172. mipImage = image->GetNextLevel(); image = mipImage;
  173. levelData = image->GetData();
  174. levelWidth = image->GetWidth();
  175. levelHeight = image->GetHeight();
  176. }
  177. switch (components)
  178. {
  179. case 1:
  180. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  181. break;
  182. case 2:
  183. format = Graphics::GetLuminanceAlphaFormat();
  184. break;
  185. case 3:
  186. format = Graphics::GetRGBFormat();
  187. break;
  188. case 4:
  189. format = Graphics::GetRGBAFormat();
  190. break;
  191. default:
  192. assert(false); // Should not reach here
  193. break;
  194. }
  195. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  196. if (IsCompressed() && requestedLevels_ > 1)
  197. requestedLevels_ = 0;
  198. SetSize(levelWidth, levelHeight, format);
  199. if (!object_.name_)
  200. return false;
  201. for (unsigned i = 0; i < levels_; ++i)
  202. {
  203. SetData(i, 0, 0, levelWidth, levelHeight, levelData);
  204. memoryUse += levelWidth * levelHeight * components;
  205. if (i < levels_ - 1)
  206. {
  207. mipImage = image->GetNextLevel(); image = mipImage;
  208. levelData = image->GetData();
  209. levelWidth = image->GetWidth();
  210. levelHeight = image->GetHeight();
  211. }
  212. }
  213. }
  214. else
  215. {
  216. int width = image->GetWidth();
  217. int height = image->GetHeight();
  218. unsigned levels = image->GetNumCompressedLevels();
  219. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  220. bool needDecompress = false;
  221. if (!format)
  222. {
  223. format = Graphics::GetRGBAFormat();
  224. needDecompress = true;
  225. }
  226. unsigned mipsToSkip = mipsToSkip_[quality];
  227. if (mipsToSkip >= levels)
  228. mipsToSkip = levels - 1;
  229. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  230. --mipsToSkip;
  231. width /= (1 << mipsToSkip);
  232. height /= (1 << mipsToSkip);
  233. SetNumLevels(Max((levels - mipsToSkip), 1U));
  234. SetSize(width, height, format);
  235. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  236. {
  237. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  238. if (!needDecompress)
  239. {
  240. SetData(i, 0, 0, level.width_, level.height_, level.data_);
  241. memoryUse += level.rows_ * level.rowSize_;
  242. }
  243. else
  244. {
  245. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  246. level.Decompress(rgbaData);
  247. SetData(i, 0, 0, level.width_, level.height_, rgbaData);
  248. memoryUse += level.width_ * level.height_ * 4;
  249. delete[] rgbaData;
  250. }
  251. }
  252. }
  253. SetMemoryUse(memoryUse);
  254. return true;
  255. }
  256. bool Texture2D::GetData(unsigned level, void* dest) const
  257. {
  258. if (!object_.name_ || !graphics_)
  259. {
  260. ATOMIC_LOGERROR("No texture created, can not get data");
  261. return false;
  262. }
  263. #ifndef GL_ES_VERSION_2_0
  264. if (!dest)
  265. {
  266. ATOMIC_LOGERROR("Null destination for getting data");
  267. return false;
  268. }
  269. if (level >= levels_)
  270. {
  271. ATOMIC_LOGERROR("Illegal mip level for getting data");
  272. return false;
  273. }
  274. if (graphics_->IsDeviceLost())
  275. {
  276. ATOMIC_LOGWARNING("Getting texture data while device is lost");
  277. return false;
  278. }
  279. graphics_->SetTextureForUpdate(const_cast<Texture2D*>(this));
  280. if (!IsCompressed())
  281. glGetTexImage(target_, level, GetExternalFormat(format_), GetDataType(format_), dest);
  282. else
  283. glGetCompressedTexImage(target_, level, dest);
  284. graphics_->SetTexture(0, 0);
  285. return true;
  286. #else
  287. // Special case on GLES: if the texture is a rendertarget, can make it current and use glReadPixels()
  288. if (usage_ == TEXTURE_RENDERTARGET)
  289. {
  290. graphics_->SetRenderTarget(0, const_cast<Texture2D*>(this));
  291. // Ensure the FBO is current; this viewport is actually never rendered to
  292. graphics_->SetViewport(IntRect(0, 0, width_, height_));
  293. glReadPixels(0, 0, width_, height_, GetExternalFormat(format_), GetDataType(format_), dest);
  294. return true;
  295. }
  296. ATOMIC_LOGERROR("Getting texture data not supported");
  297. return false;
  298. #endif
  299. }
  300. bool Texture2D::Create()
  301. {
  302. Release();
  303. if (!graphics_ || !width_ || !height_)
  304. return false;
  305. if (graphics_->IsDeviceLost())
  306. {
  307. ATOMIC_LOGWARNING("Texture creation while device is lost");
  308. return true;
  309. }
  310. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  311. unsigned externalFormat = GetExternalFormat(format_);
  312. unsigned dataType = GetDataType(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);
  325. return true;
  326. }
  327. else
  328. return false;
  329. }
  330. glGenTextures(1, &object_.name_);
  331. // Ensure that our texture is bound to OpenGL texture unit 0
  332. graphics_->SetTextureForUpdate(this);
  333. // If not compressed, create the initial level 0 texture with null data
  334. bool success = true;
  335. if (!IsCompressed())
  336. {
  337. glGetError();
  338. glTexImage2D(target_, 0, format, width_, height_, 0, externalFormat, dataType, 0);
  339. if (glGetError())
  340. {
  341. ATOMIC_LOGERROR("Failed to create texture");
  342. success = false;
  343. }
  344. }
  345. // Set mipmapping
  346. levels_ = CheckMaxLevels(width_, height_, requestedLevels_);
  347. #ifndef GL_ES_VERSION_2_0
  348. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  349. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  350. #endif
  351. // Set initial parameters, then unbind the texture
  352. UpdateParameters();
  353. graphics_->SetTexture(0, 0);
  354. return success;
  355. }
  356. }