2
0

OGLTexture3D.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. //
  2. // Copyright (c) 2008-2013 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 "Context.h"
  24. #include "FileSystem.h"
  25. #include "Graphics.h"
  26. #include "GraphicsEvents.h"
  27. #include "GraphicsImpl.h"
  28. #include "Log.h"
  29. #include "Profiler.h"
  30. #include "Renderer.h"
  31. #include "ResourceCache.h"
  32. #include "Texture3D.h"
  33. #include "XMLFile.h"
  34. #include "DebugNew.h"
  35. namespace Urho3D
  36. {
  37. Texture3D::Texture3D(Context* context) :
  38. Texture(context)
  39. {
  40. target_ = GL_TEXTURE_3D;
  41. }
  42. Texture3D::~Texture3D()
  43. {
  44. Release();
  45. }
  46. void Texture3D::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<Texture3D>();
  49. }
  50. bool Texture3D::Load(Deserializer& source)
  51. {
  52. PROFILE(LoadTexture3D);
  53. // In headless mode, do not actually load the texture, just return success
  54. if (!graphics_)
  55. return true;
  56. // If device is lost, retry later
  57. if (graphics_->IsDeviceLost())
  58. {
  59. LOGWARNING("Texture load while device is lost");
  60. dataPending_ = true;
  61. return true;
  62. }
  63. // If over the texture budget, see if materials can be freed to allow textures to be freed
  64. CheckTextureBudget(GetTypeStatic());
  65. // Before actually loading the texture, get optional parameters from an XML description file
  66. LoadParameters();
  67. String texPath, texName, texExt;
  68. SplitPath(GetName(), texPath, texName, texExt);
  69. SharedPtr<XMLFile> xml(new XMLFile(context_));
  70. if (!xml->Load(source))
  71. return false;
  72. XMLElement textureElem = xml->GetRoot();
  73. XMLElement volumeElem = textureElem.GetChild("volume");
  74. XMLElement colorlutElem = textureElem.GetChild("colorlut");
  75. if (volumeElem)
  76. {
  77. String name = volumeElem.GetAttribute("name");
  78. String volumeTexPath, volumeTexName, volumeTexExt;
  79. SplitPath(name, volumeTexPath, volumeTexName, volumeTexExt);
  80. // If path is empty, add the XML file path
  81. if (volumeTexPath.Empty())
  82. name = texPath + name;
  83. SharedPtr<Image> image(GetSubsystem<ResourceCache>()->GetResource<Image>(name));
  84. return Load(image);
  85. }
  86. else if (colorlutElem)
  87. {
  88. String name = colorlutElem.GetAttribute("name");
  89. String colorlutTexPath, colorlutTexName, colorlutTexExt;
  90. SplitPath(name, colorlutTexPath, colorlutTexName, colorlutTexExt);
  91. // If path is empty, add the XML file path
  92. if (colorlutTexPath.Empty())
  93. name = texPath + name;
  94. SharedPtr<File> file = GetSubsystem<ResourceCache>()->GetFile(name);
  95. SharedPtr<Image> image(new Image(context_));
  96. if (!image->LoadColorLUT(*(file.Get())))
  97. return false;
  98. return Load(image);
  99. }
  100. return false;
  101. }
  102. void Texture3D::OnDeviceLost()
  103. {
  104. GPUObject::OnDeviceLost();
  105. if (renderSurface_)
  106. renderSurface_->OnDeviceLost();
  107. }
  108. void Texture3D::OnDeviceReset()
  109. {
  110. // If has a file name, reload through the resource cache. Otherwise just recreate.
  111. if (!object_)
  112. {
  113. if (!GetName().Trimmed().Empty())
  114. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  115. else
  116. {
  117. Create();
  118. dataLost_ = true;
  119. }
  120. }
  121. else if (dataPending_)
  122. {
  123. if (!GetName().Trimmed().Empty())
  124. dataLost_ = !GetSubsystem<ResourceCache>()->ReloadResource(this);
  125. else
  126. dataLost_ = true;
  127. }
  128. dataPending_ = false;
  129. }
  130. void Texture3D::Release()
  131. {
  132. if (object_)
  133. {
  134. if (!graphics_ || graphics_->IsDeviceLost())
  135. return;
  136. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  137. {
  138. if (graphics_->GetTexture(i) == this)
  139. graphics_->SetTexture(i, 0);
  140. }
  141. if (renderSurface_)
  142. renderSurface_->Release();
  143. glDeleteTextures(1, &object_);
  144. object_ = 0;
  145. }
  146. else
  147. {
  148. if (renderSurface_)
  149. renderSurface_->Release();
  150. }
  151. }
  152. bool Texture3D::SetSize(int width, int height, int depth, unsigned format, TextureUsage usage)
  153. {
  154. // Delete the old rendersurface if any
  155. renderSurface_.Reset();
  156. usage_ = usage;
  157. if (usage >= TEXTURE_RENDERTARGET)
  158. {
  159. renderSurface_ = new RenderSurface(this);
  160. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  161. addressMode_[COORD_U] = ADDRESS_CLAMP;
  162. addressMode_[COORD_V] = ADDRESS_CLAMP;
  163. filterMode_ = FILTER_NEAREST;
  164. requestedLevels_ = 1;
  165. }
  166. if (usage == TEXTURE_RENDERTARGET)
  167. SubscribeToEvent(E_RENDERSURFACEUPDATE, HANDLER(Texture3D, HandleRenderSurfaceUpdate));
  168. else
  169. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  170. width_ = width;
  171. height_ = height;
  172. depth_ = depth;
  173. format_ = format;
  174. return Create();
  175. }
  176. bool Texture3D::SetData(unsigned level, int x, int y, int z, int width, int height, int depth, const void* data)
  177. {
  178. if (!object_ || !graphics_)
  179. {
  180. LOGERROR("No texture created, can not set data");
  181. return false;
  182. }
  183. if (!data)
  184. {
  185. LOGERROR("Null source for setting data");
  186. return false;
  187. }
  188. if (level >= levels_)
  189. {
  190. LOGERROR("Illegal mip level for setting data");
  191. return false;
  192. }
  193. if (graphics_->IsDeviceLost())
  194. {
  195. LOGWARNING("Texture data assignment while device is lost");
  196. dataPending_ = true;
  197. return true;
  198. }
  199. if (IsCompressed())
  200. {
  201. x &= ~3;
  202. y &= ~3;
  203. }
  204. int levelWidth = GetLevelWidth(level);
  205. int levelHeight = GetLevelHeight(level);
  206. int levelDepth = GetLevelDepth(level);
  207. if (x < 0 || x + width > levelWidth || y < 0 || y + height > levelHeight || z < 0 || z + depth > levelDepth || width <= 0 || height <= 0 || depth <= 0)
  208. {
  209. LOGERROR("Illegal dimensions for setting data");
  210. return false;
  211. }
  212. graphics_->SetTextureForUpdate(this);
  213. bool wholeLevel = x == 0 && y == 0 && z == 0 && width == levelWidth && height == levelHeight && depth == levelDepth;
  214. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  215. if (!IsCompressed())
  216. {
  217. if (wholeLevel)
  218. glTexImage3D(target_, level, format, width, height, depth, 0, GetExternalFormat(format_), GetDataType(format_), data);
  219. else
  220. glTexSubImage3D(target_, level, x, y, z, width, height, depth, GetExternalFormat(format_), GetDataType(format_), data);
  221. }
  222. else
  223. {
  224. if (wholeLevel)
  225. glCompressedTexImage3D(target_, level, format, width, height, depth, 0, GetDataSize(width, height, depth), data);
  226. else
  227. glCompressedTexSubImage3D(target_, level, x, y, z, width, height, depth, format, GetDataSize(width, height, depth), data);
  228. }
  229. graphics_->SetTexture(0, 0);
  230. return true;
  231. }
  232. bool Texture3D::Load(SharedPtr<Image> image, bool useAlpha)
  233. {
  234. if (!image)
  235. {
  236. LOGERROR("Null image, can not load texture");
  237. return false;
  238. }
  239. unsigned memoryUse = sizeof(Texture3D);
  240. int quality = QUALITY_HIGH;
  241. Renderer* renderer = GetSubsystem<Renderer>();
  242. if (renderer)
  243. quality = renderer->GetTextureQuality();
  244. if (!image->IsCompressed())
  245. {
  246. unsigned char* levelData = image->GetData();
  247. int levelWidth = image->GetWidth();
  248. int levelHeight = image->GetHeight();
  249. int levelDepth = image->GetDepth();
  250. unsigned components = image->GetComponents();
  251. unsigned format = 0;
  252. // Discard unnecessary mip levels
  253. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  254. {
  255. image = image->GetNextLevel();
  256. levelData = image->GetData();
  257. levelWidth = image->GetWidth();
  258. levelHeight = image->GetHeight();
  259. levelDepth = image->GetDepth();
  260. }
  261. switch (components)
  262. {
  263. case 1:
  264. format = useAlpha ? Graphics::GetAlphaFormat() : Graphics::GetLuminanceFormat();
  265. break;
  266. case 2:
  267. format = Graphics::GetLuminanceAlphaFormat();
  268. break;
  269. case 3:
  270. format = Graphics::GetRGBFormat();
  271. break;
  272. case 4:
  273. format = Graphics::GetRGBAFormat();
  274. break;
  275. }
  276. SetSize(levelWidth, levelHeight, levelDepth, format);
  277. if (!object_)
  278. return false;
  279. for (unsigned i = 0; i < levels_; ++i)
  280. {
  281. SetData(i, 0, 0, 0, levelWidth, levelHeight, levelDepth, levelData);
  282. memoryUse += levelWidth * levelHeight * levelDepth * components;
  283. if (i < levels_ - 1)
  284. {
  285. image = image->GetNextLevel();
  286. levelData = image->GetData();
  287. levelWidth = image->GetWidth();
  288. levelHeight = image->GetHeight();
  289. levelDepth = image->GetDepth();
  290. }
  291. }
  292. }
  293. else
  294. {
  295. int width = image->GetWidth();
  296. int height = image->GetHeight();
  297. int depth = image->GetDepth();
  298. unsigned levels = image->GetNumCompressedLevels();
  299. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  300. bool needDecompress = false;
  301. if (!format)
  302. {
  303. format = Graphics::GetRGBAFormat();
  304. needDecompress = true;
  305. }
  306. unsigned mipsToSkip = mipsToSkip_[quality];
  307. if (mipsToSkip >= levels)
  308. mipsToSkip = levels - 1;
  309. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4 || depth / (1 << mipsToSkip) < 4))
  310. --mipsToSkip;
  311. width /= (1 << mipsToSkip);
  312. height /= (1 << mipsToSkip);
  313. depth /= (1 << mipsToSkip);
  314. SetNumLevels(Max((int)(levels - mipsToSkip), 1));
  315. SetSize(width, height, depth, format);
  316. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  317. {
  318. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  319. if (!needDecompress)
  320. {
  321. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, level.data_);
  322. memoryUse += level.depth_ * level.rows_ * level.rowSize_;
  323. }
  324. else
  325. {
  326. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * level.depth_ * 4];
  327. level.Decompress(rgbaData);
  328. SetData(i, 0, 0, 0, level.width_, level.height_, level.depth_, rgbaData);
  329. memoryUse += level.width_ * level.height_ * level.depth_ * 4;
  330. delete[] rgbaData;
  331. }
  332. }
  333. }
  334. SetMemoryUse(memoryUse);
  335. return true;
  336. }
  337. bool Texture3D::GetData(unsigned level, void* dest) const
  338. {
  339. #ifndef GL_ES_VERSION_2_0
  340. if (!object_ || !graphics_)
  341. {
  342. LOGERROR("No texture created, can not get data");
  343. return false;
  344. }
  345. if (!dest)
  346. {
  347. LOGERROR("Null destination for getting data");
  348. return false;
  349. }
  350. if (level >= levels_)
  351. {
  352. LOGERROR("Illegal mip level for getting data");
  353. return false;
  354. }
  355. if (graphics_->IsDeviceLost())
  356. {
  357. LOGWARNING("Getting texture data while device is lost");
  358. return false;
  359. }
  360. graphics_->SetTextureForUpdate(const_cast<Texture3D*>(this));
  361. if (!IsCompressed())
  362. glGetTexImage(target_, level, GetExternalFormat(format_), GetDataType(format_), dest);
  363. else
  364. glGetCompressedTexImage(target_, level, dest);
  365. graphics_->SetTexture(0, 0);
  366. return true;
  367. #else
  368. LOGERROR("Getting texture data not supported");
  369. return false;
  370. #endif
  371. }
  372. bool Texture3D::Create()
  373. {
  374. Release();
  375. if (!graphics_ || !width_ || !height_ || !depth_)
  376. return false;
  377. if (graphics_->IsDeviceLost())
  378. {
  379. LOGWARNING("Texture creation while device is lost");
  380. return true;
  381. }
  382. unsigned format = GetSRGB() ? GetSRGBFormat(format_) : format_;
  383. unsigned externalFormat = GetExternalFormat(format_);
  384. unsigned dataType = GetDataType(format_);
  385. glGenTextures(1, &object_);
  386. // Ensure that our texture is bound to OpenGL texture unit 0
  387. graphics_->SetTextureForUpdate(this);
  388. // If not compressed, create the initial level 0 texture with null data
  389. bool success = true;
  390. if (!IsCompressed())
  391. {
  392. glGetError();
  393. glTexImage3D(target_, 0, format, width_, height_, depth_, 0, externalFormat, dataType, 0);
  394. if (glGetError())
  395. {
  396. LOGERROR("Failed to create texture");
  397. success = false;
  398. }
  399. }
  400. // Set mipmapping
  401. levels_ = requestedLevels_;
  402. if (!levels_)
  403. {
  404. unsigned maxSize = Max(Max((int)width_, (int)height_), (int)depth_);
  405. while (maxSize)
  406. {
  407. maxSize >>= 1;
  408. ++levels_;
  409. }
  410. }
  411. #ifndef GL_ES_VERSION_2_0
  412. glTexParameteri(target_, GL_TEXTURE_BASE_LEVEL, 0);
  413. glTexParameteri(target_, GL_TEXTURE_MAX_LEVEL, levels_ - 1);
  414. #endif
  415. // Set initial parameters, then unbind the texture
  416. UpdateParameters();
  417. graphics_->SetTexture(0, 0);
  418. return success;
  419. }
  420. void Texture3D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  421. {
  422. if (renderSurface_ && renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS)
  423. renderSurface_->QueueUpdate();
  424. }
  425. }