OGLTexture3D.cpp 12 KB

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