Texture2D.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 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, int multiSample, bool autoResolve)
  94. {
  95. if (width <= 0 || height <= 0)
  96. {
  97. ATOMIC_LOGERROR("Zero or negative texture dimensions");
  98. return false;
  99. }
  100. multiSample = Clamp(multiSample, 1, 16);
  101. if (multiSample == 1)
  102. autoResolve = false;
  103. else if (multiSample > 1 && usage < TEXTURE_RENDERTARGET)
  104. {
  105. ATOMIC_LOGERROR("Multisampling is only supported for rendertarget or depth-stencil textures");
  106. return false;
  107. }
  108. // Disable mipmaps if multisample & custom resolve
  109. if (multiSample > 1 && autoResolve == false)
  110. requestedLevels_ = 1;
  111. // Delete the old rendersurface if any
  112. renderSurface_.Reset();
  113. usage_ = usage;
  114. if (usage >= TEXTURE_RENDERTARGET)
  115. {
  116. renderSurface_ = new RenderSurface(this);
  117. // Clamp mode addressing by default and nearest filtering
  118. addressMode_[COORD_U] = ADDRESS_CLAMP;
  119. addressMode_[COORD_V] = ADDRESS_CLAMP;
  120. filterMode_ = FILTER_NEAREST;
  121. }
  122. if (usage == TEXTURE_RENDERTARGET)
  123. SubscribeToEvent(E_RENDERSURFACEUPDATE, ATOMIC_HANDLER(Texture2D, HandleRenderSurfaceUpdate));
  124. else
  125. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  126. width_ = width;
  127. height_ = height;
  128. format_ = format;
  129. depth_ = 1;
  130. multiSample_ = multiSample;
  131. autoResolve_ = autoResolve;
  132. return Create();
  133. }
  134. SharedPtr<Image> Texture2D::GetImage() const
  135. {
  136. if (format_ != Graphics::GetRGBAFormat() && format_ != Graphics::GetRGBFormat())
  137. {
  138. ATOMIC_LOGERROR("Unsupported texture format, can not convert to Image");
  139. return SharedPtr<Image>();
  140. }
  141. Image* rawImage = new Image(context_);
  142. if (format_ == Graphics::GetRGBAFormat())
  143. rawImage->SetSize(width_, height_, 4);
  144. else if (format_ == Graphics::GetRGBFormat())
  145. rawImage->SetSize(width_, height_, 3);
  146. else
  147. assert(0);
  148. GetData(0, rawImage->GetData());
  149. return SharedPtr<Image>(rawImage);
  150. }
  151. void Texture2D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  152. {
  153. if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))
  154. {
  155. Renderer* renderer = GetSubsystem<Renderer>();
  156. if (renderer)
  157. renderer->QueueRenderSurface(renderSurface_);
  158. renderSurface_->ResetUpdateQueued();
  159. }
  160. }
  161. }