OGLTexture3D.cpp 11 KB

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