OGLTexture2DArray.cpp 14 KB

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