Texture2DArray.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Copyright (c) 2008-2018 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/Texture2DArray.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. #ifdef _MSC_VER
  36. #pragma warning(disable:4355)
  37. #endif
  38. namespace Urho3D
  39. {
  40. Texture2DArray::Texture2DArray(Context* context) :
  41. Texture(context)
  42. {
  43. #ifdef URHO3D_OPENGL
  44. #ifndef GL_ES_VERSION_2_0
  45. target_ = GL_TEXTURE_2D_ARRAY;
  46. #endif
  47. #endif
  48. }
  49. Texture2DArray::~Texture2DArray()
  50. {
  51. Release();
  52. }
  53. void Texture2DArray::RegisterObject(Context* context)
  54. {
  55. context->RegisterFactory<Texture2DArray>();
  56. }
  57. bool Texture2DArray::BeginLoad(Deserializer& source)
  58. {
  59. auto* cache = GetSubsystem<ResourceCache>();
  60. // In headless mode, do not actually load the texture, just return success
  61. if (!graphics_)
  62. return true;
  63. // If device is lost, retry later
  64. if (graphics_->IsDeviceLost())
  65. {
  66. URHO3D_LOGWARNING("Texture load while device is lost");
  67. dataPending_ = true;
  68. return true;
  69. }
  70. cache->ResetDependencies(this);
  71. String texPath, texName, texExt;
  72. SplitPath(GetName(), texPath, texName, texExt);
  73. loadParameters_ = (new XMLFile(context_));
  74. if (!loadParameters_->Load(source))
  75. {
  76. loadParameters_.Reset();
  77. return false;
  78. }
  79. loadImages_.Clear();
  80. XMLElement textureElem = loadParameters_->GetRoot();
  81. XMLElement layerElem = textureElem.GetChild("layer");
  82. while (layerElem)
  83. {
  84. String name = layerElem.GetAttribute("name");
  85. // If path is empty, add the XML file path
  86. if (GetPath(name).Empty())
  87. name = texPath + name;
  88. loadImages_.Push(cache->GetTempResource<Image>(name));
  89. cache->StoreResourceDependency(this, name);
  90. layerElem = layerElem.GetNext("layer");
  91. }
  92. // Precalculate mip levels if async loading
  93. if (GetAsyncLoadState() == ASYNC_LOADING)
  94. {
  95. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  96. {
  97. if (loadImages_[i])
  98. loadImages_[i]->PrecalculateLevels();
  99. }
  100. }
  101. return true;
  102. }
  103. bool Texture2DArray::EndLoad()
  104. {
  105. // In headless mode, do not actually load the texture, just return success
  106. if (!graphics_ || graphics_->IsDeviceLost())
  107. return true;
  108. // If over the texture budget, see if materials can be freed to allow textures to be freed
  109. CheckTextureBudget(GetTypeStatic());
  110. SetParameters(loadParameters_);
  111. SetLayers(loadImages_.Size());
  112. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  113. SetData(i, loadImages_[i]);
  114. loadImages_.Clear();
  115. loadParameters_.Reset();
  116. return true;
  117. }
  118. void Texture2DArray::SetLayers(unsigned layers)
  119. {
  120. Release();
  121. layers_ = layers;
  122. }
  123. bool Texture2DArray::SetSize(unsigned layers, int width, int height, unsigned format, TextureUsage usage)
  124. {
  125. if (width <= 0 || height <= 0)
  126. {
  127. URHO3D_LOGERROR("Zero or negative texture array size");
  128. return false;
  129. }
  130. if (usage == TEXTURE_DEPTHSTENCIL)
  131. {
  132. URHO3D_LOGERROR("Depth-stencil usage not supported for texture arrays");
  133. return false;
  134. }
  135. // Delete the old rendersurface if any
  136. renderSurface_.Reset();
  137. usage_ = usage;
  138. if (usage == TEXTURE_RENDERTARGET)
  139. {
  140. renderSurface_ = new RenderSurface(this);
  141. // Nearest filtering by default
  142. filterMode_ = FILTER_NEAREST;
  143. }
  144. if (usage == TEXTURE_RENDERTARGET)
  145. SubscribeToEvent(E_RENDERSURFACEUPDATE, URHO3D_HANDLER(Texture2DArray, HandleRenderSurfaceUpdate));
  146. else
  147. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  148. width_ = width;
  149. height_ = height;
  150. format_ = format;
  151. depth_ = 1;
  152. if (layers)
  153. layers_ = layers;
  154. layerMemoryUse_.Resize(layers_);
  155. for (unsigned i = 0; i < layers_; ++i)
  156. layerMemoryUse_[i] = 0;
  157. return Create();
  158. }
  159. void Texture2DArray::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  160. {
  161. if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))
  162. {
  163. auto* renderer = GetSubsystem<Renderer>();
  164. if (renderer)
  165. renderer->QueueRenderSurface(renderSurface_);
  166. renderSurface_->ResetUpdateQueued();
  167. }
  168. }
  169. }