Texture3D.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright (c) 2008-2023 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. Texture3D::Texture3D(Context* context) :
  19. Texture(context)
  20. {
  21. #ifdef URHO3D_OPENGL
  22. #ifndef URHO3D_GLES2
  23. if (Graphics::GetGAPI() == GAPI_OPENGL)
  24. target_ = GL_TEXTURE_3D;
  25. #endif
  26. #endif
  27. }
  28. Texture3D::~Texture3D()
  29. {
  30. Release();
  31. }
  32. void Texture3D::RegisterObject(Context* context)
  33. {
  34. context->RegisterFactory<Texture3D>();
  35. }
  36. bool Texture3D::BeginLoad(Deserializer& source)
  37. {
  38. auto* cache = GetSubsystem<ResourceCache>();
  39. // In headless mode, do not actually load the texture, just return success
  40. if (!graphics_)
  41. return true;
  42. // If device is lost, retry later
  43. if (graphics_->IsDeviceLost())
  44. {
  45. URHO3D_LOGWARNING("Texture load while device is lost");
  46. dataPending_ = true;
  47. return true;
  48. }
  49. String texPath, texName, texExt;
  50. SplitPath(GetName(), texPath, texName, texExt);
  51. cache->ResetDependencies(this);
  52. loadParameters_ = new XMLFile(context_);
  53. if (!loadParameters_->Load(source))
  54. {
  55. loadParameters_.Reset();
  56. return false;
  57. }
  58. XMLElement textureElem = loadParameters_->GetRoot();
  59. XMLElement volumeElem = textureElem.GetChild("volume");
  60. XMLElement colorlutElem = textureElem.GetChild("colorlut");
  61. if (volumeElem)
  62. {
  63. String name = volumeElem.GetAttribute("name");
  64. String volumeTexPath, volumeTexName, volumeTexExt;
  65. SplitPath(name, volumeTexPath, volumeTexName, volumeTexExt);
  66. // If path is empty, add the XML file path
  67. if (volumeTexPath.Empty())
  68. name = texPath + name;
  69. loadImage_ = cache->GetTempResource<Image>(name);
  70. // Precalculate mip levels if async loading
  71. if (loadImage_ && GetAsyncLoadState() == ASYNC_LOADING)
  72. loadImage_->PrecalculateLevels();
  73. cache->StoreResourceDependency(this, name);
  74. return true;
  75. }
  76. else if (colorlutElem)
  77. {
  78. String name = colorlutElem.GetAttribute("name");
  79. String colorlutTexPath, colorlutTexName, colorlutTexExt;
  80. SplitPath(name, colorlutTexPath, colorlutTexName, colorlutTexExt);
  81. // If path is empty, add the XML file path
  82. if (colorlutTexPath.Empty())
  83. name = texPath + name;
  84. SharedPtr<File> file = GetSubsystem<ResourceCache>()->GetFile(name);
  85. loadImage_ = new Image(context_);
  86. if (!loadImage_->LoadColorLUT(*(file.Get())))
  87. {
  88. loadParameters_.Reset();
  89. loadImage_.Reset();
  90. return false;
  91. }
  92. // Precalculate mip levels if async loading
  93. if (loadImage_ && GetAsyncLoadState() == ASYNC_LOADING)
  94. loadImage_->PrecalculateLevels();
  95. cache->StoreResourceDependency(this, name);
  96. return true;
  97. }
  98. URHO3D_LOGERROR("Texture3D XML data for " + GetName() + " did not contain either volume or colorlut element");
  99. return false;
  100. }
  101. bool Texture3D::EndLoad()
  102. {
  103. // In headless mode, do not actually load the texture, just return success
  104. if (!graphics_ || graphics_->IsDeviceLost())
  105. return true;
  106. // If over the texture budget, see if materials can be freed to allow textures to be freed
  107. CheckTextureBudget(GetTypeStatic());
  108. SetParameters(loadParameters_);
  109. bool success = SetData(loadImage_);
  110. loadImage_.Reset();
  111. loadParameters_.Reset();
  112. return success;
  113. }
  114. bool Texture3D::SetSize(int width, int height, int depth, unsigned format, TextureUsage usage)
  115. {
  116. if (width <= 0 || height <= 0 || depth <= 0)
  117. {
  118. URHO3D_LOGERROR("Zero or negative 3D texture dimensions");
  119. return false;
  120. }
  121. if (usage >= TEXTURE_RENDERTARGET)
  122. {
  123. URHO3D_LOGERROR("Rendertarget or depth-stencil usage not supported for 3D textures");
  124. return false;
  125. }
  126. usage_ = usage;
  127. width_ = width;
  128. height_ = height;
  129. depth_ = depth;
  130. format_ = format;
  131. return Create();
  132. }
  133. void Texture3D::OnDeviceLost()
  134. {
  135. GAPI gapi = Graphics::GetGAPI();
  136. #ifdef URHO3D_OPENGL
  137. if (gapi == GAPI_OPENGL)
  138. return OnDeviceLost_OGL();
  139. #endif
  140. #ifdef URHO3D_D3D11
  141. if (gapi == GAPI_D3D11)
  142. return OnDeviceLost_D3D11();
  143. #endif
  144. }
  145. void Texture3D::OnDeviceReset()
  146. {
  147. GAPI gapi = Graphics::GetGAPI();
  148. #ifdef URHO3D_OPENGL
  149. if (gapi == GAPI_OPENGL)
  150. return OnDeviceReset_OGL();
  151. #endif
  152. #ifdef URHO3D_D3D11
  153. if (gapi == GAPI_D3D11)
  154. return OnDeviceReset_D3D11();
  155. #endif
  156. }
  157. void Texture3D::Release()
  158. {
  159. GAPI gapi = Graphics::GetGAPI();
  160. #ifdef URHO3D_OPENGL
  161. if (gapi == GAPI_OPENGL)
  162. return Release_OGL();
  163. #endif
  164. #ifdef URHO3D_D3D11
  165. if (gapi == GAPI_D3D11)
  166. return Release_D3D11();
  167. #endif
  168. }
  169. bool Texture3D::SetData(unsigned level, int x, int y, int z, int width, int height, int depth, const void* data)
  170. {
  171. GAPI gapi = Graphics::GetGAPI();
  172. #ifdef URHO3D_OPENGL
  173. if (gapi == GAPI_OPENGL)
  174. return SetData_OGL(level, x, y, z, width, height, depth, data);
  175. #endif
  176. #ifdef URHO3D_D3D11
  177. if (gapi == GAPI_D3D11)
  178. return SetData_D3D11(level, x, y, z, width, height, depth, data);
  179. #endif
  180. return {}; // Prevent warning
  181. }
  182. bool Texture3D::SetData(Image* image, bool useAlpha)
  183. {
  184. GAPI gapi = Graphics::GetGAPI();
  185. #ifdef URHO3D_OPENGL
  186. if (gapi == GAPI_OPENGL)
  187. return SetData_OGL(image, useAlpha);
  188. #endif
  189. #ifdef URHO3D_D3D11
  190. if (gapi == GAPI_D3D11)
  191. return SetData_D3D11(image, useAlpha);
  192. #endif
  193. return {}; // Prevent warning
  194. }
  195. bool Texture3D::GetData(unsigned level, void* dest) const
  196. {
  197. GAPI gapi = Graphics::GetGAPI();
  198. #ifdef URHO3D_OPENGL
  199. if (gapi == GAPI_OPENGL)
  200. return GetData_OGL(level, dest);
  201. #endif
  202. #ifdef URHO3D_D3D11
  203. if (gapi == GAPI_D3D11)
  204. return GetData_D3D11(level, dest);
  205. #endif
  206. return {}; // Prevent warning
  207. }
  208. bool Texture3D::Create()
  209. {
  210. GAPI gapi = Graphics::GetGAPI();
  211. #ifdef URHO3D_OPENGL
  212. if (gapi == GAPI_OPENGL)
  213. return Create_OGL();
  214. #endif
  215. #ifdef URHO3D_D3D11
  216. if (gapi == GAPI_D3D11)
  217. return Create_D3D11();
  218. #endif
  219. return {}; // Prevent warning
  220. }
  221. }