PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/Urho3D/Graphics/OpenGL/OGLTexture2DArray.cpp

https://gitlab.com/Teo-Mirror/Urho3D
C++ | 484 lines | 379 code | 69 blank | 36 comment | 104 complexity | 27d2a9eb3850153c60dda2620bac3b4d MD5 | raw file
  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/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. }
  82. bool Texture2DArray::SetData(unsigned layer, unsigned level, int x, int y, int width, int height, const void* data)
  83. {
  84. URHO3D_PROFILE(SetTextureData);
  85. if (!object_.name_ || !graphics_)
  86. {
  87. URHO3D_LOGERROR("Texture array not created, can not set data");
  88. return false;
  89. }
  90. if (!data)
  91. {
  92. URHO3D_LOGERROR("Null source for setting data");
  93. return false;
  94. }
  95. if (layer >= layers_)
  96. {
  97. URHO3D_LOGERROR("Illegal layer for setting data");
  98. return false;
  99. }
  100. if (level >= levels_)
  101. {
  102. URHO3D_LOGERROR("Illegal mip level for setting data");
  103. return false;
  104. }
  105. if (graphics_->IsDeviceLost())
  106. {
  107. URHO3D_LOGWARNING("Texture array data assignment while device is lost");
  108. dataPending_ = true;
  109. return true;
  110. }
  111. if (IsCompressed())
  112. {
  113. x &= ~3;
  114. y &= ~3;
  115. }
  116. int levelWidth = GetLevelWidth(level);
  117. int levelHeight = GetLevelHeight(level);
  118. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || width <= 0 || height <= 0)
  119. {
  120. URHO3D_LOGERROR("Illegal dimensions for setting data");
  121. return false;
  122. }
  123. graphics_->SetTextureForUpdate(this);
  124. #ifndef GL_ES_VERSION_2_0
  125. bool wholeLevel = x == 0 && y == 0 && width == levelWidth && height == levelHeight && layer == 0;
  126. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  127. if (!IsCompressed())
  128. {
  129. if (wholeLevel)
  130. glTexImage3D(target_, level, format, width, height, layers_, 0, GetExternalFormat(format_),
  131. GetDataType(format_), 0);
  132. glTexSubImage3D(target_, level, x, y, layer, width, height, 1, GetExternalFormat(format_),
  133. GetDataType(format_), data);
  134. }
  135. else
  136. {
  137. if (wholeLevel)
  138. glCompressedTexImage3D(target_, level, format, width, height, layers_, 0,
  139. GetDataSize(width, height, layers_), 0);
  140. glCompressedTexSubImage3D(target_, level, x, y, layer, width, height, 1, format,
  141. GetDataSize(width, height), data);
  142. }
  143. #endif
  144. graphics_->SetTexture(0, 0);
  145. return true;
  146. }
  147. bool Texture2DArray::SetData(unsigned layer, Deserializer& source)
  148. {
  149. SharedPtr<Image> image(new Image(context_));
  150. if (!image->Load(source))
  151. return false;
  152. return SetData(layer, image);
  153. }
  154. bool Texture2DArray::SetData(unsigned layer, Image* image, bool useAlpha)
  155. {
  156. if (!image)
  157. {
  158. URHO3D_LOGERROR("Null image, can not set data");
  159. return false;
  160. }
  161. if (!layers_)
  162. {
  163. URHO3D_LOGERROR("Number of layers in the array must be set first");
  164. return false;
  165. }
  166. if (layer >= layers_)
  167. {
  168. URHO3D_LOGERROR("Illegal layer for setting data");
  169. return false;
  170. }
  171. // Use a shared ptr for managing the temporary mip images created during this function
  172. SharedPtr<Image> mipImage;
  173. unsigned memoryUse = 0;
  174. int quality = QUALITY_HIGH;
  175. Renderer* renderer = GetSubsystem<Renderer>();
  176. if (renderer)
  177. quality = renderer->GetTextureQuality();
  178. if (!image->IsCompressed())
  179. {
  180. // Convert unsuitable formats to RGBA
  181. unsigned components = image->GetComponents();
  182. if (Graphics::GetGL3Support() && ((components == 1 && !useAlpha) || components == 2))
  183. {
  184. mipImage = image->ConvertToRGBA(); image = mipImage;
  185. if (!image)
  186. return false;
  187. components = image->GetComponents();
  188. }
  189. unsigned char* levelData = image->GetData();
  190. int levelWidth = image->GetWidth();
  191. int levelHeight = image->GetHeight();
  192. unsigned format = 0;
  193. // Discard unnecessary mip levels
  194. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  195. {
  196. mipImage = image->GetNextLevel(); image = mipImage;
  197. levelData = image->GetData();
  198. levelWidth = image->GetWidth();
  199. levelHeight = image->GetHeight();
  200. }
  201. switch (components)
  202. {
  203. case 1:
  204. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  205. break;
  206. case 2:
  207. format = Graphics::GetLuminanceAlphaFormat();
  208. break;
  209. case 3:
  210. format = Graphics::GetRGBFormat();
  211. break;
  212. case 4:
  213. format = Graphics::GetRGBAFormat();
  214. break;
  215. default:
  216. assert(false); // Should not reach here
  217. break;
  218. }
  219. // Create the texture array when layer 0 is being loaded, check that rest of the layers are same size & format
  220. if (!layer)
  221. {
  222. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  223. if (IsCompressed() && requestedLevels_ > 1)
  224. requestedLevels_ = 0;
  225. // Create the texture array (the number of layers must have been already set)
  226. SetSize(0, levelWidth, levelHeight, format);
  227. }
  228. else
  229. {
  230. if (!object_.name_)
  231. {
  232. URHO3D_LOGERROR("Texture array layer 0 must be loaded first");
  233. return false;
  234. }
  235. if (levelWidth != width_ || levelHeight != height_ || format != format_)
  236. {
  237. URHO3D_LOGERROR("Texture array layer does not match size or format of layer 0");
  238. return false;
  239. }
  240. }
  241. for (unsigned i = 0; i < levels_; ++i)
  242. {
  243. SetData(layer, i, 0, 0, levelWidth, levelHeight, levelData);
  244. memoryUse += levelWidth * levelHeight * components;
  245. if (i < levels_ - 1)
  246. {
  247. mipImage = image->GetNextLevel(); image = mipImage;
  248. levelData = image->GetData();
  249. levelWidth = image->GetWidth();
  250. levelHeight = image->GetHeight();
  251. }
  252. }
  253. }
  254. else
  255. {
  256. int width = image->GetWidth();
  257. int height = image->GetHeight();
  258. unsigned levels = image->GetNumCompressedLevels();
  259. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  260. bool needDecompress = false;
  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 array when layer 0 is being loaded, assume rest of the layers are same size & format
  274. if (!layer)
  275. {
  276. SetNumLevels(Max((levels - mipsToSkip), 1U));
  277. SetSize(0, width, height, format);
  278. }
  279. else
  280. {
  281. if (!object_.name_)
  282. {
  283. URHO3D_LOGERROR("Texture array layer 0 must be loaded first");
  284. return false;
  285. }
  286. if (width != width_ || height != height_ || format != format_)
  287. {
  288. URHO3D_LOGERROR("Texture array layer does not match size or format of layer 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(layer, 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(layer, i, 0, 0, level.width_, level.height_, rgbaData);
  305. memoryUse += level.width_ * level.height_ * 4;
  306. delete[] rgbaData;
  307. }
  308. }
  309. }
  310. layerMemoryUse_[layer] = memoryUse;
  311. unsigned totalMemoryUse = sizeof(Texture2DArray) + layerMemoryUse_.Capacity() * sizeof(unsigned);
  312. for (unsigned i = 0; i < layers_; ++i)
  313. totalMemoryUse += layerMemoryUse_[i];
  314. SetMemoryUse(totalMemoryUse);
  315. return true;
  316. }
  317. bool Texture2DArray::GetData(unsigned layer, unsigned level, void* dest) const
  318. {
  319. #ifndef GL_ES_VERSION_2_0
  320. if (!object_.name_ || !graphics_)
  321. {
  322. URHO3D_LOGERROR("Texture array not created, can not get data");
  323. return false;
  324. }
  325. if (!dest)
  326. {
  327. URHO3D_LOGERROR("Null destination for getting data");
  328. return false;
  329. }
  330. if (layer != 0)
  331. {
  332. URHO3D_LOGERROR("Only the full download of the array is supported, set layer=0");
  333. return false;
  334. }
  335. if (level >= levels_)
  336. {
  337. URHO3D_LOGERROR("Illegal mip level for getting data");
  338. return false;
  339. }
  340. if (graphics_->IsDeviceLost())
  341. {
  342. URHO3D_LOGWARNING("Getting texture data while device is lost");
  343. return false;
  344. }
  345. graphics_->SetTextureForUpdate(const_cast<Texture2DArray*>(this));
  346. if (!IsCompressed())
  347. glGetTexImage(target_, level, GetExternalFormat(format_), GetDataType(format_), dest);
  348. else
  349. glGetCompressedTexImage(target_, level, dest);
  350. graphics_->SetTexture(0, 0);
  351. return true;
  352. #else
  353. URHO3D_LOGERROR("Getting texture data not supported");
  354. return false;
  355. #endif
  356. }
  357. bool Texture2DArray::Create()
  358. {
  359. Release();
  360. #ifdef GL_ES_VERSION_2_0
  361. URHO3D_LOGERROR("Failed to create 2D array texture, currently unsupported on OpenGL ES 2");
  362. return false;
  363. #else
  364. if (!graphics_ || !width_ || !height_ || !layers_)
  365. return false;
  366. if (graphics_->IsDeviceLost())
  367. {
  368. URHO3D_LOGWARNING("Texture array 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. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  375. unsigned externalFormat = GetExternalFormat(format_);
  376. unsigned dataType = GetDataType(format_);
  377. // If not compressed, create the initial level 0 texture with null data
  378. bool success = true;
  379. if (!IsCompressed())
  380. {
  381. glGetError();
  382. glTexImage3D(target_, 0, format, width_, height_, layers_, 0, externalFormat, dataType, 0);
  383. if (glGetError())
  384. success = false;
  385. }
  386. if (!success)
  387. URHO3D_LOGERROR("Failed to create texture array");
  388. // Set mipmapping
  389. if (usage_ == TEXTURE_DEPTHSTENCIL)
  390. requestedLevels_ = 1;
  391. else if (usage_ == TEXTURE_RENDERTARGET)
  392. {
  393. #ifdef __EMSCRIPTEN__
  394. // glGenerateMipmap appears to not be working on WebGL, disable rendertarget mipmaps for now
  395. requestedLevels_ = 1;
  396. #else
  397. if (requestedLevels_ != 1)
  398. {
  399. // Generate levels for the first time now
  400. RegenerateLevels();
  401. // Determine max. levels automatically
  402. requestedLevels_ = 0;
  403. }
  404. #endif
  405. }
  406. levels_ = CheckMaxLevels(width_, height_, requestedLevels_);
  407. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  408. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  409. // Set initial parameters, then unbind the texture
  410. UpdateParameters();
  411. graphics_->SetTexture(0, 0);
  412. return success;
  413. #endif
  414. }
  415. }