Texture2DArray.cpp 5.6 KB

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