Texture2D.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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/Texture2D.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. Texture2D::Texture2D(Context* context) :
  19. Texture(context)
  20. {
  21. #ifdef URHO3D_OPENGL
  22. if (Graphics::GetGAPI() == GAPI_OPENGL)
  23. target_ = GL_TEXTURE_2D;
  24. #endif
  25. }
  26. Texture2D::~Texture2D()
  27. {
  28. Release();
  29. }
  30. void Texture2D::RegisterObject(Context* context)
  31. {
  32. context->RegisterFactory<Texture2D>();
  33. }
  34. bool Texture2D::BeginLoad(Deserializer& source)
  35. {
  36. // In headless mode, do not actually load the texture, just return success
  37. if (!graphics_)
  38. return true;
  39. // If device is lost, retry later
  40. if (graphics_->IsDeviceLost())
  41. {
  42. URHO3D_LOGWARNING("Texture load while device is lost");
  43. dataPending_ = true;
  44. return true;
  45. }
  46. // Load the image data for EndLoad()
  47. loadImage_ = new Image(context_);
  48. if (!loadImage_->Load(source))
  49. {
  50. loadImage_.Reset();
  51. return false;
  52. }
  53. // Precalculate mip levels if async loading
  54. if (GetAsyncLoadState() == ASYNC_LOADING)
  55. loadImage_->PrecalculateLevels();
  56. // Load the optional parameters file
  57. auto* cache = GetSubsystem<ResourceCache>();
  58. String xmlName = ReplaceExtension(GetName(), ".xml");
  59. loadParameters_ = cache->GetTempResource<XMLFile>(xmlName, false);
  60. return true;
  61. }
  62. bool Texture2D::EndLoad()
  63. {
  64. // In headless mode, do not actually load the texture, just return success
  65. if (!graphics_ || graphics_->IsDeviceLost())
  66. return true;
  67. // If over the texture budget, see if materials can be freed to allow textures to be freed
  68. CheckTextureBudget(GetTypeStatic());
  69. SetParameters(loadParameters_);
  70. bool success = SetData(loadImage_);
  71. loadImage_.Reset();
  72. loadParameters_.Reset();
  73. return success;
  74. }
  75. bool Texture2D::SetSize(int width, int height, unsigned format, TextureUsage usage, int multiSample, bool autoResolve)
  76. {
  77. if (width <= 0 || height <= 0)
  78. {
  79. URHO3D_LOGERROR("Zero or negative texture dimensions");
  80. return false;
  81. }
  82. multiSample = Clamp(multiSample, 1, 16);
  83. if (multiSample == 1)
  84. autoResolve = false;
  85. else if (multiSample > 1 && usage < TEXTURE_RENDERTARGET)
  86. {
  87. URHO3D_LOGERROR("Multisampling is only supported for rendertarget or depth-stencil textures");
  88. return false;
  89. }
  90. // Disable mipmaps if multisample & custom resolve
  91. if (multiSample > 1 && autoResolve == false)
  92. requestedLevels_ = 1;
  93. // Delete the old rendersurface if any
  94. renderSurface_.Reset();
  95. usage_ = usage;
  96. if (usage >= TEXTURE_RENDERTARGET)
  97. {
  98. renderSurface_ = new RenderSurface(this);
  99. // Clamp mode addressing by default and nearest filtering
  100. addressModes_[COORD_U] = ADDRESS_CLAMP;
  101. addressModes_[COORD_V] = ADDRESS_CLAMP;
  102. filterMode_ = FILTER_NEAREST;
  103. }
  104. if (usage == TEXTURE_RENDERTARGET)
  105. SubscribeToEvent(E_RENDERSURFACEUPDATE, URHO3D_HANDLER(Texture2D, HandleRenderSurfaceUpdate));
  106. else
  107. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  108. width_ = width;
  109. height_ = height;
  110. format_ = format;
  111. depth_ = 1;
  112. multiSample_ = multiSample;
  113. autoResolve_ = autoResolve;
  114. return Create();
  115. }
  116. bool Texture2D::GetImage(Image& image) const
  117. {
  118. if (format_ != Graphics::GetRGBAFormat() && format_ != Graphics::GetRGBFormat())
  119. {
  120. URHO3D_LOGERROR("Unsupported texture format, can not convert to Image");
  121. return false;
  122. }
  123. image.SetSize(width_, height_, GetComponents());
  124. GetData(0, image.GetData());
  125. return true;
  126. }
  127. SharedPtr<Image> Texture2D::GetImage() const
  128. {
  129. auto rawImage = MakeShared<Image>(context_);
  130. if (!GetImage(*rawImage))
  131. return nullptr;
  132. return rawImage;
  133. }
  134. void Texture2D::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  135. {
  136. if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))
  137. {
  138. auto* renderer = GetSubsystem<Renderer>();
  139. if (renderer)
  140. renderer->QueueRenderSurface(renderSurface_);
  141. renderSurface_->ResetUpdateQueued();
  142. }
  143. }
  144. void Texture2D::OnDeviceLost()
  145. {
  146. GAPI gapi = Graphics::GetGAPI();
  147. #ifdef URHO3D_OPENGL
  148. if (gapi == GAPI_OPENGL)
  149. return OnDeviceLost_OGL();
  150. #endif
  151. #ifdef URHO3D_D3D11
  152. if (gapi == GAPI_D3D11)
  153. return OnDeviceLost_D3D11();
  154. #endif
  155. }
  156. void Texture2D::OnDeviceReset()
  157. {
  158. GAPI gapi = Graphics::GetGAPI();
  159. #ifdef URHO3D_OPENGL
  160. if (gapi == GAPI_OPENGL)
  161. return OnDeviceReset_OGL();
  162. #endif
  163. #ifdef URHO3D_D3D11
  164. if (gapi == GAPI_D3D11)
  165. return OnDeviceReset_D3D11();
  166. #endif
  167. }
  168. void Texture2D::Release()
  169. {
  170. GAPI gapi = Graphics::GetGAPI();
  171. #ifdef URHO3D_OPENGL
  172. if (gapi == GAPI_OPENGL)
  173. return Release_OGL();
  174. #endif
  175. #ifdef URHO3D_D3D11
  176. if (gapi == GAPI_D3D11)
  177. return Release_D3D11();
  178. #endif
  179. }
  180. bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data)
  181. {
  182. GAPI gapi = Graphics::GetGAPI();
  183. #ifdef URHO3D_OPENGL
  184. if (gapi == GAPI_OPENGL)
  185. return SetData_OGL(level, x, y, width, height, data);
  186. #endif
  187. #ifdef URHO3D_D3D11
  188. if (gapi == GAPI_D3D11)
  189. return SetData_D3D11(level, x, y, width, height, data);
  190. #endif
  191. return {}; // Prevent warning
  192. }
  193. bool Texture2D::SetData(Image* image, bool useAlpha)
  194. {
  195. GAPI gapi = Graphics::GetGAPI();
  196. #ifdef URHO3D_OPENGL
  197. if (gapi == GAPI_OPENGL)
  198. return SetData_OGL(image, useAlpha);
  199. #endif
  200. #ifdef URHO3D_D3D11
  201. if (gapi == GAPI_D3D11)
  202. return SetData_D3D11(image, useAlpha);
  203. #endif
  204. return {}; // Prevent warning
  205. }
  206. bool Texture2D::GetData(unsigned level, void* dest) const
  207. {
  208. GAPI gapi = Graphics::GetGAPI();
  209. #ifdef URHO3D_OPENGL
  210. if (gapi == GAPI_OPENGL)
  211. return GetData_OGL(level, dest);
  212. #endif
  213. #ifdef URHO3D_D3D11
  214. if (gapi == GAPI_D3D11)
  215. return GetData_D3D11(level, dest);
  216. #endif
  217. return {}; // Prevent warning
  218. }
  219. bool Texture2D::Create()
  220. {
  221. GAPI gapi = Graphics::GetGAPI();
  222. #ifdef URHO3D_OPENGL
  223. if (gapi == GAPI_OPENGL)
  224. return Create_OGL();
  225. #endif
  226. #ifdef URHO3D_D3D11
  227. if (gapi == GAPI_D3D11)
  228. return Create_D3D11();
  229. #endif
  230. return {}; // Prevent warning
  231. }
  232. }