OGLTexture3D.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //
  2. // Copyright (c) 2008-2020 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/Texture3D.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 Urho3D
  36. {
  37. void Texture3D::OnDeviceLost()
  38. {
  39. if (object_.name_ && !graphics_->IsDeviceLost())
  40. glDeleteTextures(1, &object_.name_);
  41. GPUObject::OnDeviceLost();
  42. }
  43. void Texture3D::OnDeviceReset()
  44. {
  45. if (!object_.name_ || dataPending_)
  46. {
  47. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  48. auto* 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 Texture3D::Release()
  60. {
  61. if (object_.name_)
  62. {
  63. if (!graphics_ || graphics_->IsDeviceLost())
  64. return;
  65. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  66. {
  67. if (graphics_->GetTexture(i) == this)
  68. graphics_->SetTexture(i, nullptr);
  69. }
  70. glDeleteTextures(1, &object_.name_);
  71. object_.name_ = 0;
  72. }
  73. }
  74. bool Texture3D::SetData(unsigned level, int x, int y, int z, int width, int height, int depth, const void* data)
  75. {
  76. URHO3D_PROFILE(SetTextureData);
  77. if (!object_.name_ || !graphics_)
  78. {
  79. URHO3D_LOGERROR("No texture created, can not set data");
  80. return false;
  81. }
  82. if (!data)
  83. {
  84. URHO3D_LOGERROR("Null source for setting data");
  85. return false;
  86. }
  87. if (level >= levels_)
  88. {
  89. URHO3D_LOGERROR("Illegal mip level for setting data");
  90. return false;
  91. }
  92. if (graphics_->IsDeviceLost())
  93. {
  94. URHO3D_LOGWARNING("Texture data assignment while device is lost");
  95. dataPending_ = true;
  96. return true;
  97. }
  98. if (IsCompressed())
  99. {
  100. x &= ~3u;
  101. y &= ~3u;
  102. }
  103. int levelWidth = GetLevelWidth(level);
  104. int levelHeight = GetLevelHeight(level);
  105. int levelDepth = GetLevelDepth(level);
  106. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || z < 0 || z + depth > levelDepth || width <= 0 ||
  107. height <= 0 || depth <= 0)
  108. {
  109. URHO3D_LOGERROR("Illegal dimensions for setting data");
  110. return false;
  111. }
  112. graphics_->SetTextureForUpdate(this);
  113. #ifndef GL_ES_VERSION_2_0
  114. bool wholeLevel = x == 0 && y == 0 && z == 0 && width == levelWidth && height == levelHeight && depth == levelDepth;
  115. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  116. if (!IsCompressed())
  117. {
  118. if (wholeLevel)
  119. glTexImage3D(target_, level, format, width, height, depth, 0, GetExternalFormat(format_), GetDataType(format_), data);
  120. else
  121. glTexSubImage3D(target_, level, x, y, z, width, height, depth, GetExternalFormat(format_), GetDataType(format_), data);
  122. }
  123. else
  124. {
  125. if (wholeLevel)
  126. glCompressedTexImage3D(target_, level, format, width, height, depth, 0, GetDataSize(width, height, depth), data);
  127. else
  128. glCompressedTexSubImage3D(target_, level, x, y, z, width, height, depth, format, GetDataSize(width, height, depth),
  129. data);
  130. }
  131. #endif
  132. graphics_->SetTexture(0, nullptr);
  133. return true;
  134. }
  135. bool Texture3D::SetData(Image* image, bool useAlpha)
  136. {
  137. if (!image)
  138. {
  139. URHO3D_LOGERROR("Null image, can not set data");
  140. return false;
  141. }
  142. // Use a shared ptr for managing the temporary mip images created during this function
  143. SharedPtr<Image> mipImage;
  144. unsigned memoryUse = sizeof(Texture3D);
  145. MaterialQuality quality = QUALITY_HIGH;
  146. auto* renderer = GetSubsystem<Renderer>();
  147. if (renderer)
  148. quality = renderer->GetTextureQuality();
  149. if (!image->IsCompressed())
  150. {
  151. // Convert unsuitable formats to RGBA
  152. unsigned components = image->GetComponents();
  153. if (Graphics::GetGL3Support() && ((components == 1 && !useAlpha) || components == 2))
  154. {
  155. mipImage = image->ConvertToRGBA(); image = mipImage;
  156. if (!image)
  157. return false;
  158. components = image->GetComponents();
  159. }
  160. unsigned char* levelData = image->GetData();
  161. int levelWidth = image->GetWidth();
  162. int levelHeight = image->GetHeight();
  163. int levelDepth = image->GetDepth();
  164. unsigned format = 0;
  165. // Discard unnecessary mip levels
  166. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  167. {
  168. mipImage = image->GetNextLevel(); image = mipImage;
  169. levelData = image->GetData();
  170. levelWidth = image->GetWidth();
  171. levelHeight = image->GetHeight();
  172. levelDepth = image->GetDepth();
  173. }
  174. switch (components)
  175. {
  176. case 1:
  177. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  178. break;
  179. case 2:
  180. format = Graphics::GetLuminanceAlphaFormat();
  181. break;
  182. case 3:
  183. format = Graphics::GetRGBFormat();
  184. break;
  185. case 4:
  186. format = Graphics::GetRGBAFormat();
  187. break;
  188. default:
  189. assert(false); // Should not reach here
  190. break;
  191. }
  192. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  193. if (IsCompressed() && requestedLevels_ > 1)
  194. requestedLevels_ = 0;
  195. SetSize(levelWidth, levelHeight, levelDepth, format);
  196. if (!object_.name_)
  197. return false;
  198. for (unsigned i = 0; i < levels_; ++i)
  199. {
  200. SetData(i, 0, 0, 0, levelWidth, levelHeight, levelDepth, levelData);
  201. memoryUse += levelWidth * levelHeight * levelDepth * components;
  202. if (i < levels_ - 1)
  203. {
  204. mipImage = image->GetNextLevel(); image = mipImage;
  205. levelData = image->GetData();
  206. levelWidth = image->GetWidth();
  207. levelHeight = image->GetHeight();
  208. levelDepth = image->GetDepth();
  209. }
  210. }
  211. }
  212. else
  213. {
  214. int width = image->GetWidth();
  215. int height = image->GetHeight();
  216. int depth = image->GetDepth();
  217. unsigned levels = image->GetNumCompressedLevels();
  218. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  219. bool needDecompress = false;
  220. if (!format)
  221. {
  222. format = Graphics::GetRGBAFormat();
  223. needDecompress = true;
  224. }
  225. unsigned mipsToSkip = mipsToSkip_[quality];
  226. if (mipsToSkip >= levels)
  227. mipsToSkip = levels - 1;
  228. while (mipsToSkip && (width / (1u << mipsToSkip) < 4 || height / (1u << mipsToSkip) < 4 || depth / (1u << mipsToSkip) < 4))
  229. --mipsToSkip;
  230. width /= (1u << mipsToSkip);
  231. height /= (1u << mipsToSkip);
  232. depth /= (1u << mipsToSkip);
  233. SetNumLevels(Max((levels - mipsToSkip), 1U));
  234. SetSize(width, height, depth, 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, 0, level.width_, level.height_, level.depth_, level.data_);
  241. memoryUse += level.depth_ * level.rows_ * level.rowSize_;
  242. }
  243. else
  244. {
  245. auto* rgbaData = new unsigned char[level.width_ * level.height_ * level.depth_ * 4];
  246. level.Decompress(rgbaData);
  247. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, rgbaData);
  248. memoryUse += level.width_ * level.height_ * level.depth_ * 4;
  249. delete[] rgbaData;
  250. }
  251. }
  252. }
  253. SetMemoryUse(memoryUse);
  254. return true;
  255. }
  256. bool Texture3D::GetData(unsigned level, void* dest) const
  257. {
  258. #ifndef GL_ES_VERSION_2_0
  259. if (!object_.name_ || !graphics_)
  260. {
  261. URHO3D_LOGERROR("No texture created, can not get data");
  262. return false;
  263. }
  264. if (!dest)
  265. {
  266. URHO3D_LOGERROR("Null destination for getting data");
  267. return false;
  268. }
  269. if (level >= levels_)
  270. {
  271. URHO3D_LOGERROR("Illegal mip level for getting data");
  272. return false;
  273. }
  274. if (graphics_->IsDeviceLost())
  275. {
  276. URHO3D_LOGWARNING("Getting texture data while device is lost");
  277. return false;
  278. }
  279. graphics_->SetTextureForUpdate(const_cast<Texture3D*>(this));
  280. if (!IsCompressed())
  281. glGetTexImage(target_, level, GetExternalFormat(format_), GetDataType(format_), dest);
  282. else
  283. glGetCompressedTexImage(target_, level, dest);
  284. graphics_->SetTexture(0, nullptr);
  285. return true;
  286. #else
  287. URHO3D_LOGERROR("Getting texture data not supported");
  288. return false;
  289. #endif
  290. }
  291. bool Texture3D::Create()
  292. {
  293. Release();
  294. #ifdef GL_ES_VERSION_2_0
  295. URHO3D_LOGERROR("Failed to create 3D texture, currently unsupported on OpenGL ES 2");
  296. return false;
  297. #else
  298. if (!graphics_ || !width_ || !height_ || !depth_)
  299. return false;
  300. if (graphics_->IsDeviceLost())
  301. {
  302. URHO3D_LOGWARNING("Texture creation while device is lost");
  303. return true;
  304. }
  305. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  306. unsigned externalFormat = GetExternalFormat(format_);
  307. unsigned dataType = GetDataType(format_);
  308. glGenTextures(1, &object_.name_);
  309. // Ensure that our texture is bound to OpenGL texture unit 0
  310. graphics_->SetTextureForUpdate(this);
  311. // If not compressed, create the initial level 0 texture with null data
  312. bool success = true;
  313. if (!IsCompressed())
  314. {
  315. glGetError();
  316. glTexImage3D(target_, 0, format, width_, height_, depth_, 0, externalFormat, dataType, nullptr);
  317. if (glGetError())
  318. {
  319. URHO3D_LOGERROR("Failed to create texture");
  320. success = false;
  321. }
  322. }
  323. // Set mipmapping
  324. levels_ = CheckMaxLevels(width_, height_, depth_, requestedLevels_);
  325. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  326. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  327. // Set initial parameters, then unbind the texture
  328. UpdateParameters();
  329. graphics_->SetTexture(0, nullptr);
  330. return success;
  331. #endif
  332. }
  333. }