Texture2DArray.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. //
  2. // Copyright (c) 2008-2022 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 Urho3D
  39. {
  40. Texture2DArray::Texture2DArray(Context* context) :
  41. Texture(context)
  42. {
  43. #ifdef URHO3D_OPENGL
  44. #ifndef GL_ES_VERSION_2_0
  45. if (Graphics::GetGAPI() == GAPI_OPENGL)
  46. target_ = GL_TEXTURE_2D_ARRAY;
  47. #endif
  48. #endif
  49. }
  50. Texture2DArray::~Texture2DArray()
  51. {
  52. Release();
  53. }
  54. void Texture2DArray::RegisterObject(Context* context)
  55. {
  56. context->RegisterFactory<Texture2DArray>();
  57. }
  58. bool Texture2DArray::BeginLoad(Deserializer& source)
  59. {
  60. auto* cache = GetSubsystem<ResourceCache>();
  61. // In headless mode, do not actually load the texture, just return success
  62. if (!graphics_)
  63. return true;
  64. // If device is lost, retry later
  65. if (graphics_->IsDeviceLost())
  66. {
  67. URHO3D_LOGWARNING("Texture load while device is lost");
  68. dataPending_ = true;
  69. return true;
  70. }
  71. cache->ResetDependencies(this);
  72. String texPath, texName, texExt;
  73. SplitPath(GetName(), texPath, texName, texExt);
  74. loadParameters_ = (new XMLFile(context_));
  75. if (!loadParameters_->Load(source))
  76. {
  77. loadParameters_.Reset();
  78. return false;
  79. }
  80. loadImages_.Clear();
  81. XMLElement textureElem = loadParameters_->GetRoot();
  82. XMLElement layerElem = textureElem.GetChild("layer");
  83. while (layerElem)
  84. {
  85. String name = layerElem.GetAttribute("name");
  86. // If path is empty, add the XML file path
  87. if (GetPath(name).Empty())
  88. name = texPath + name;
  89. loadImages_.Push(cache->GetTempResource<Image>(name));
  90. cache->StoreResourceDependency(this, name);
  91. layerElem = layerElem.GetNext("layer");
  92. }
  93. // Precalculate mip levels if async loading
  94. if (GetAsyncLoadState() == ASYNC_LOADING)
  95. {
  96. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  97. {
  98. if (loadImages_[i])
  99. loadImages_[i]->PrecalculateLevels();
  100. }
  101. }
  102. return true;
  103. }
  104. bool Texture2DArray::EndLoad()
  105. {
  106. // In headless mode, do not actually load the texture, just return success
  107. if (!graphics_ || graphics_->IsDeviceLost())
  108. return true;
  109. // If over the texture budget, see if materials can be freed to allow textures to be freed
  110. CheckTextureBudget(GetTypeStatic());
  111. SetParameters(loadParameters_);
  112. SetLayers(loadImages_.Size());
  113. for (unsigned i = 0; i < loadImages_.Size(); ++i)
  114. SetData(i, loadImages_[i]);
  115. loadImages_.Clear();
  116. loadParameters_.Reset();
  117. return true;
  118. }
  119. void Texture2DArray::SetLayers(unsigned layers)
  120. {
  121. Release();
  122. layers_ = layers;
  123. }
  124. bool Texture2DArray::SetSize(unsigned layers, int width, int height, unsigned format, TextureUsage usage)
  125. {
  126. if (width <= 0 || height <= 0)
  127. {
  128. URHO3D_LOGERROR("Zero or negative texture array size");
  129. return false;
  130. }
  131. if (usage == TEXTURE_DEPTHSTENCIL)
  132. {
  133. URHO3D_LOGERROR("Depth-stencil usage not supported for texture arrays");
  134. return false;
  135. }
  136. // Delete the old rendersurface if any
  137. renderSurface_.Reset();
  138. usage_ = usage;
  139. if (usage == TEXTURE_RENDERTARGET)
  140. {
  141. renderSurface_ = new RenderSurface(this);
  142. // Nearest filtering by default
  143. filterMode_ = FILTER_NEAREST;
  144. }
  145. if (usage == TEXTURE_RENDERTARGET)
  146. SubscribeToEvent(E_RENDERSURFACEUPDATE, URHO3D_HANDLER(Texture2DArray, HandleRenderSurfaceUpdate));
  147. else
  148. UnsubscribeFromEvent(E_RENDERSURFACEUPDATE);
  149. width_ = width;
  150. height_ = height;
  151. format_ = format;
  152. depth_ = 1;
  153. if (layers)
  154. layers_ = layers;
  155. layerMemoryUse_.Resize(layers_);
  156. for (unsigned i = 0; i < layers_; ++i)
  157. layerMemoryUse_[i] = 0;
  158. return Create();
  159. }
  160. void Texture2DArray::HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData)
  161. {
  162. if (renderSurface_ && (renderSurface_->GetUpdateMode() == SURFACE_UPDATEALWAYS || renderSurface_->IsUpdateQueued()))
  163. {
  164. auto* renderer = GetSubsystem<Renderer>();
  165. if (renderer)
  166. renderer->QueueRenderSurface(renderSurface_);
  167. renderSurface_->ResetUpdateQueued();
  168. }
  169. }
  170. void Texture2DArray::OnDeviceLost()
  171. {
  172. GAPI gapi = Graphics::GetGAPI();
  173. #ifdef URHO3D_OPENGL
  174. if (gapi == GAPI_OPENGL)
  175. return OnDeviceLost_OGL();
  176. #endif
  177. #ifdef URHO3D_D3D9
  178. if (gapi == GAPI_D3D9)
  179. return OnDeviceLost_D3D9();
  180. #endif
  181. #ifdef URHO3D_D3D11
  182. if (gapi == GAPI_D3D11)
  183. return OnDeviceLost_D3D11();
  184. #endif
  185. }
  186. void Texture2DArray::OnDeviceReset()
  187. {
  188. GAPI gapi = Graphics::GetGAPI();
  189. #ifdef URHO3D_OPENGL
  190. if (gapi == GAPI_OPENGL)
  191. return OnDeviceReset_OGL();
  192. #endif
  193. #ifdef URHO3D_D3D9
  194. if (gapi == GAPI_D3D9)
  195. return OnDeviceReset_D3D9();
  196. #endif
  197. #ifdef URHO3D_D3D11
  198. if (gapi == GAPI_D3D11)
  199. return OnDeviceReset_D3D11();
  200. #endif
  201. }
  202. void Texture2DArray::Release()
  203. {
  204. GAPI gapi = Graphics::GetGAPI();
  205. #ifdef URHO3D_OPENGL
  206. if (gapi == GAPI_OPENGL)
  207. return Release_OGL();
  208. #endif
  209. #ifdef URHO3D_D3D9
  210. if (gapi == GAPI_D3D9)
  211. return Release_D3D9();
  212. #endif
  213. #ifdef URHO3D_D3D11
  214. if (gapi == GAPI_D3D11)
  215. return Release_D3D11();
  216. #endif
  217. }
  218. bool Texture2DArray::SetData(unsigned layer, unsigned level, int x, int y, int width, int height, const void* data)
  219. {
  220. GAPI gapi = Graphics::GetGAPI();
  221. #ifdef URHO3D_OPENGL
  222. if (gapi == GAPI_OPENGL)
  223. return SetData_OGL(layer, level, x, y, width, height, data);
  224. #endif
  225. #ifdef URHO3D_D3D9
  226. if (gapi == GAPI_D3D9)
  227. return SetData_D3D9(layer, level, x, y, width, height, data);
  228. #endif
  229. #ifdef URHO3D_D3D11
  230. if (gapi == GAPI_D3D11)
  231. return SetData_D3D11(layer, level, x, y, width, height, data);
  232. #endif
  233. }
  234. bool Texture2DArray::SetData(unsigned layer, Deserializer& source)
  235. {
  236. GAPI gapi = Graphics::GetGAPI();
  237. #ifdef URHO3D_OPENGL
  238. if (gapi == GAPI_OPENGL)
  239. return SetData_OGL(layer, source);
  240. #endif
  241. #ifdef URHO3D_D3D9
  242. if (gapi == GAPI_D3D9)
  243. return SetData_D3D9(layer, source);
  244. #endif
  245. #ifdef URHO3D_D3D11
  246. if (gapi == GAPI_D3D11)
  247. return SetData_D3D11(layer, source);
  248. #endif
  249. }
  250. bool Texture2DArray::SetData(unsigned layer, Image* image, bool useAlpha)
  251. {
  252. GAPI gapi = Graphics::GetGAPI();
  253. #ifdef URHO3D_OPENGL
  254. if (gapi == GAPI_OPENGL)
  255. return SetData_OGL(layer, image, useAlpha);
  256. #endif
  257. #ifdef URHO3D_D3D9
  258. if (gapi == GAPI_D3D9)
  259. return SetData_D3D9(layer, image, useAlpha);
  260. #endif
  261. #ifdef URHO3D_D3D11
  262. if (gapi == GAPI_D3D11)
  263. return SetData_D3D11(layer, image, useAlpha);
  264. #endif
  265. }
  266. bool Texture2DArray::GetData(unsigned layer, unsigned level, void* dest) const
  267. {
  268. GAPI gapi = Graphics::GetGAPI();
  269. #ifdef URHO3D_OPENGL
  270. if (gapi == GAPI_OPENGL)
  271. return GetData_OGL(layer, level, dest);
  272. #endif
  273. #ifdef URHO3D_D3D9
  274. if (gapi == GAPI_D3D9)
  275. return GetData_D3D9(layer, level, dest);
  276. #endif
  277. #ifdef URHO3D_D3D11
  278. if (gapi == GAPI_D3D11)
  279. return GetData_D3D11(layer, level, dest);
  280. #endif
  281. }
  282. bool Texture2DArray::Create()
  283. {
  284. GAPI gapi = Graphics::GetGAPI();
  285. #ifdef URHO3D_OPENGL
  286. if (gapi == GAPI_OPENGL)
  287. return Create_OGL();
  288. #endif
  289. #ifdef URHO3D_D3D9
  290. if (gapi == GAPI_D3D9)
  291. return Create_D3D9();
  292. #endif
  293. #ifdef URHO3D_D3D11
  294. if (gapi == GAPI_D3D11)
  295. return Create_D3D11();
  296. #endif
  297. }
  298. }