2
0

Texture2D.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // Copyright (c) 2008-2022 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_D3D9
  152. if (gapi == GAPI_D3D9)
  153. return OnDeviceLost_D3D9();
  154. #endif
  155. #ifdef URHO3D_D3D11
  156. if (gapi == GAPI_D3D11)
  157. return OnDeviceLost_D3D11();
  158. #endif
  159. }
  160. void Texture2D::OnDeviceReset()
  161. {
  162. GAPI gapi = Graphics::GetGAPI();
  163. #ifdef URHO3D_OPENGL
  164. if (gapi == GAPI_OPENGL)
  165. return OnDeviceReset_OGL();
  166. #endif
  167. #ifdef URHO3D_D3D9
  168. if (gapi == GAPI_D3D9)
  169. return OnDeviceReset_D3D9();
  170. #endif
  171. #ifdef URHO3D_D3D11
  172. if (gapi == GAPI_D3D11)
  173. return OnDeviceReset_D3D11();
  174. #endif
  175. }
  176. void Texture2D::Release()
  177. {
  178. GAPI gapi = Graphics::GetGAPI();
  179. #ifdef URHO3D_OPENGL
  180. if (gapi == GAPI_OPENGL)
  181. return Release_OGL();
  182. #endif
  183. #ifdef URHO3D_D3D9
  184. if (gapi == GAPI_D3D9)
  185. return Release_D3D9();
  186. #endif
  187. #ifdef URHO3D_D3D11
  188. if (gapi == GAPI_D3D11)
  189. return Release_D3D11();
  190. #endif
  191. }
  192. bool Texture2D::SetData(unsigned level, int x, int y, int width, int height, const void* data)
  193. {
  194. GAPI gapi = Graphics::GetGAPI();
  195. #ifdef URHO3D_OPENGL
  196. if (gapi == GAPI_OPENGL)
  197. return SetData_OGL(level, x, y, width, height, data);
  198. #endif
  199. #ifdef URHO3D_D3D9
  200. if (gapi == GAPI_D3D9)
  201. return SetData_D3D9(level, x, y, width, height, data);
  202. #endif
  203. #ifdef URHO3D_D3D11
  204. if (gapi == GAPI_D3D11)
  205. return SetData_D3D11(level, x, y, width, height, data);
  206. #endif
  207. return {}; // Prevent warning
  208. }
  209. bool Texture2D::SetData(Image* image, bool useAlpha)
  210. {
  211. GAPI gapi = Graphics::GetGAPI();
  212. #ifdef URHO3D_OPENGL
  213. if (gapi == GAPI_OPENGL)
  214. return SetData_OGL(image, useAlpha);
  215. #endif
  216. #ifdef URHO3D_D3D9
  217. if (gapi == GAPI_D3D9)
  218. return SetData_D3D9(image, useAlpha);
  219. #endif
  220. #ifdef URHO3D_D3D11
  221. if (gapi == GAPI_D3D11)
  222. return SetData_D3D11(image, useAlpha);
  223. #endif
  224. return {}; // Prevent warning
  225. }
  226. bool Texture2D::GetData(unsigned level, void* dest) const
  227. {
  228. GAPI gapi = Graphics::GetGAPI();
  229. #ifdef URHO3D_OPENGL
  230. if (gapi == GAPI_OPENGL)
  231. return GetData_OGL(level, dest);
  232. #endif
  233. #ifdef URHO3D_D3D9
  234. if (gapi == GAPI_D3D9)
  235. return GetData_D3D9(level, dest);
  236. #endif
  237. #ifdef URHO3D_D3D11
  238. if (gapi == GAPI_D3D11)
  239. return GetData_D3D11(level, dest);
  240. #endif
  241. return {}; // Prevent warning
  242. }
  243. bool Texture2D::Create()
  244. {
  245. GAPI gapi = Graphics::GetGAPI();
  246. #ifdef URHO3D_OPENGL
  247. if (gapi == GAPI_OPENGL)
  248. return Create_OGL();
  249. #endif
  250. #ifdef URHO3D_D3D9
  251. if (gapi == GAPI_D3D9)
  252. return Create_D3D9();
  253. #endif
  254. #ifdef URHO3D_D3D11
  255. if (gapi == GAPI_D3D11)
  256. return Create_D3D11();
  257. #endif
  258. return {}; // Prevent warning
  259. }
  260. }