Texture2D.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // Copyright (c) 2008-2016 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 rightsR
  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/Texture2D.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. Texture2D::Texture2D(Context* context) :
  38. Texture(context)
  39. {
  40. #ifdef ATOMIC_OPENGL
  41. target_ = GL_TEXTURE_2D;
  42. #endif
  43. }
  44. Texture2D::~Texture2D()
  45. {
  46. Release();
  47. }
  48. void Texture2D::RegisterObject(Context* context)
  49. {
  50. context->RegisterFactory<Texture2D>();
  51. }
  52. bool Texture2D::BeginLoad(Deserializer& source)
  53. {
  54. // In headless mode, do not actually load the texture, just return success
  55. if (!graphics_)
  56. return true;
  57. // If device is lost, retry later
  58. if (graphics_->IsDeviceLost())
  59. {
  60. ATOMIC_LOGWARNING("Texture load while device is lost");
  61. dataPending_ = true;
  62. return true;
  63. }
  64. // Load the image data for EndLoad()
  65. loadImage_ = new Image(context_);
  66. if (!loadImage_->Load(source))
  67. {
  68. loadImage_.Reset();
  69. return false;
  70. }
  71. // Precalculate mip levels if async loading
  72. if (GetAsyncLoadState() == ASYNC_LOADING)
  73. loadImage_->PrecalculateLevels();
  74. // Load the optional parameters file
  75. ResourceCache* cache = GetSubsystem<ResourceCache>();
  76. String xmlName = ReplaceExtension(GetName(), ".xml");
  77. loadParameters_ = cache->GetTempResource<XMLFile>(xmlName, false);
  78. return true;
  79. }
  80. bool Texture2D::EndLoad()
  81. {
  82. // In headless mode, do not actually load the texture, just return success
  83. if (!graphics_ || graphics_->IsDeviceLost())
  84. return true;
  85. // If over the texture budget, see if materials can be freed to allow textures to be freed
  86. CheckTextureBudget(GetTypeStatic());
  87. SetParameters(loadParameters_);
  88. bool success = SetData(loadImage_);
  89. loadImage_.Reset();
  90. loadParameters_.Reset();
  91. return success;
  92. }
  93. bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usage)
  94. {
  95. if (width <= 0 || height <= 0)
  96. {
  97. ATOMIC_LOGERROR("Zero or negative texture dimensions");
  98. return false;
  99. }
  100. // Delete the old rendersurface if any
  101. renderSurface_.Reset();
  102. usage_ = usage;
  103. if (usage >= TEXTURE_RENDERTARGET)
  104. {
  105. renderSurface_ = new RenderSurface(this);
  106. // Clamp mode addressing by default, nearest filtering, and mipmaps disabled
  107. addressMode_[COORD_U] = ADDRESS_CLAMP;
  108. addressMode_[COORD_V] = ADDRESS_CLAMP;
  109. filterMode_ = FILTER_NEAREST;
  110. requestedLevels_ = 1;
  111. }
  112. if (usage == TEXTURE_RENDERTARGET)
  113. SubscribeToEvent(E_RENDERSURFACEUPDATE, ATOMIC_HANDLER(Texture2D, HandleRenderSurfaceUpdate));
  114. else
  115. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  116. width_ = width;
  117. height_ = height;
  118. format_ = format;
  119. return Create();
  120. }
  121. void Texture2D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  122. {
  123. if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))
  124. {
  125. Renderer* renderer = GetSubsystem<Renderer>();
  126. if (renderer)
  127. renderer->QueueRenderSurface(renderSurface_);
  128. renderSurface_->ResetUpdateQueued();
  129. }
  130. }
  131. }