Texture3D.cpp 5.3 KB

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