Texture3D.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. Texture3D::Texture3D(Context* context) :
  38. Texture(context)
  39. {
  40. #ifdef URHO3D_OPENGL
  41. #ifndef GL_ES_VERSION_2_0
  42. target_ = GL_TEXTURE_3D;
  43. #endif
  44. #endif
  45. }
  46. Texture3D::~Texture3D()
  47. {
  48. Release();
  49. }
  50. void Texture3D::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<Texture3D>();
  53. }
  54. bool Texture3D::BeginLoad(Deserializer& source)
  55. {
  56. auto* cache = GetSubsystem<ResourceCache>();
  57. // In headless mode, do not actually load the texture, just return success
  58. if (!graphics_)
  59. return true;
  60. // If device is lost, retry later
  61. if (graphics_->IsDeviceLost())
  62. {
  63. URHO3D_LOGWARNING("Texture load while device is lost");
  64. dataPending_ = true;
  65. return true;
  66. }
  67. String texPath, texName, texExt;
  68. SplitPath(GetName(), texPath, texName, texExt);
  69. cache->ResetDependencies(this);
  70. loadParameters_ = new XMLFile(context_);
  71. if (!loadParameters_->Load(source))
  72. {
  73. loadParameters_.Reset();
  74. return false;
  75. }
  76. XMLElement textureElem = loadParameters_->GetRoot();
  77. XMLElement volumeElem = textureElem.GetChild("volume");
  78. XMLElement colorlutElem = textureElem.GetChild("colorlut");
  79. if (volumeElem)
  80. {
  81. String name = volumeElem.GetAttribute("name");
  82. String volumeTexPath, volumeTexName, volumeTexExt;
  83. SplitPath(name, volumeTexPath, volumeTexName, volumeTexExt);
  84. // If path is empty, add the XML file path
  85. if (volumeTexPath.Empty())
  86. name = texPath + name;
  87. loadImage_ = cache->GetTempResource<Image>(name);
  88. // Precalculate mip levels if async loading
  89. if (loadImage_ && GetAsyncLoadState() == ASYNC_LOADING)
  90. loadImage_->PrecalculateLevels();
  91. cache->StoreResourceDependency(this, name);
  92. return true;
  93. }
  94. else if (colorlutElem)
  95. {
  96. String name = colorlutElem.GetAttribute("name");
  97. String colorlutTexPath, colorlutTexName, colorlutTexExt;
  98. SplitPath(name, colorlutTexPath, colorlutTexName, colorlutTexExt);
  99. // If path is empty, add the XML file path
  100. if (colorlutTexPath.Empty())
  101. name = texPath + name;
  102. SharedPtr<File> file = GetSubsystem<ResourceCache>()->GetFile(name);
  103. loadImage_ = new Image(context_);
  104. if (!loadImage_->LoadColorLUT(*(file.Get())))
  105. {
  106. loadParameters_.Reset();
  107. loadImage_.Reset();
  108. return false;
  109. }
  110. // Precalculate mip levels if async loading
  111. if (loadImage_ && GetAsyncLoadState() == ASYNC_LOADING)
  112. loadImage_->PrecalculateLevels();
  113. cache->StoreResourceDependency(this, name);
  114. return true;
  115. }
  116. URHO3D_LOGERROR("Texture3D XML data for " + GetName() + " did not contain either volume or colorlut element");
  117. return false;
  118. }
  119. bool Texture3D::EndLoad()
  120. {
  121. // In headless mode, do not actually load the texture, just return success
  122. if (!graphics_ || graphics_->IsDeviceLost())
  123. return true;
  124. // If over the texture budget, see if materials can be freed to allow textures to be freed
  125. CheckTextureBudget(GetTypeStatic());
  126. SetParameters(loadParameters_);
  127. bool success = SetData(loadImage_);
  128. loadImage_.Reset();
  129. loadParameters_.Reset();
  130. return success;
  131. }
  132. bool Texture3D::SetSize(int width, int height, int depth, unsigned format, TextureUsage usage)
  133. {
  134. if (width <= 0 || height <= 0 || depth <= 0)
  135. {
  136. URHO3D_LOGERROR("Zero or negative 3D texture dimensions");
  137. return false;
  138. }
  139. if (usage >= TEXTURE_RENDERTARGET)
  140. {
  141. URHO3D_LOGERROR("Rendertarget or depth-stencil usage not supported for 3D textures");
  142. return false;
  143. }
  144. usage_ = usage;
  145. width_ = width;
  146. height_ = height;
  147. depth_ = depth;
  148. format_ = format;
  149. return Create();
  150. }
  151. }