D3D9Texture2DArray.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // Copyright (c) 2008-2020 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. void Texture2DArray::OnDeviceLost()
  41. {
  42. if (usage_ > TEXTURE_STATIC)
  43. Release();
  44. }
  45. void Texture2DArray::OnDeviceReset()
  46. {
  47. if (usage_ > TEXTURE_STATIC || !object_.ptr_ || dataPending_)
  48. {
  49. // If has a resource file, reload through the resource cache. Otherwise just recreate.
  50. ResourceCache* cache = GetSubsystem<ResourceCache>();
  51. if (cache->Exists(GetName()))
  52. dataLost_ = !cache->ReloadResource(this);
  53. if (!object_.ptr_)
  54. {
  55. Create();
  56. dataLost_ = true;
  57. }
  58. }
  59. dataPending_ = false;
  60. }
  61. void Texture2DArray::Release()
  62. {
  63. if (graphics_)
  64. {
  65. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  66. {
  67. if (graphics_->GetTexture(i) == this)
  68. graphics_->SetTexture(i, nullptr);
  69. }
  70. }
  71. if (renderSurface_)
  72. renderSurface_->Release();
  73. URHO3D_SAFE_RELEASE(object_.ptr_);
  74. }
  75. bool Texture2DArray::SetData(unsigned layer, unsigned level, int x, int y, int width, int height, const void* data)
  76. {
  77. URHO3D_LOGERROR("Texture2DArray not supported on Direct3D9, can not set data");
  78. return false;
  79. }
  80. bool Texture2DArray::SetData(unsigned layer, Deserializer& source)
  81. {
  82. SharedPtr<Image> image(new Image(context_));
  83. if (!image->Load(source))
  84. return false;
  85. return SetData(layer, image);
  86. }
  87. bool Texture2DArray::SetData(unsigned layer, Image* image, bool useAlpha)
  88. {
  89. if (!image)
  90. {
  91. URHO3D_LOGERROR("Null image, can not set data");
  92. return false;
  93. }
  94. if (!layers_)
  95. {
  96. URHO3D_LOGERROR("Number of layers in the array must be set first");
  97. return false;
  98. }
  99. if (layer >= layers_)
  100. {
  101. URHO3D_LOGERROR("Illegal layer for setting data");
  102. return false;
  103. }
  104. // Use a shared ptr for managing the temporary mip images created during this function
  105. SharedPtr<Image> mipImage;
  106. unsigned memoryUse = 0;
  107. MaterialQuality quality = QUALITY_HIGH;
  108. Renderer* renderer = GetSubsystem<Renderer>();
  109. if (renderer)
  110. quality = renderer->GetTextureQuality();
  111. if (!image->IsCompressed())
  112. {
  113. unsigned char* levelData = image->GetData();
  114. int levelWidth = image->GetWidth();
  115. int levelHeight = image->GetHeight();
  116. unsigned components = image->GetComponents();
  117. unsigned format = 0;
  118. // Discard unnecessary mip levels
  119. for (unsigned i = 0; i < mipsToSkip_[quality]; ++i)
  120. {
  121. mipImage = image->GetNextLevel(); image = mipImage;
  122. levelData = image->GetData();
  123. levelWidth = image->GetWidth();
  124. levelHeight = image->GetHeight();
  125. }
  126. switch (components)
  127. {
  128. case 1:
  129. format = Graphics::GetAlphaFormat();
  130. break;
  131. case 4:
  132. format = Graphics::GetRGBAFormat();
  133. break;
  134. default: break;
  135. }
  136. // Create the texture array when layer 0 is being loaded, check that rest of the layers are same size & format
  137. if (!layer)
  138. {
  139. // If image was previously compressed, reset number of requested levels to avoid error if level count is too high for new size
  140. if (IsCompressed() && requestedLevels_ > 1)
  141. requestedLevels_ = 0;
  142. // Create the texture array (the number of layers must have been already set)
  143. SetSize(0, levelWidth, levelHeight, format);
  144. }
  145. else
  146. {
  147. if (!object_.ptr_)
  148. {
  149. // Do not spam this error on D3D9
  150. //URHO3D_LOGERROR("Texture array layer 0 must be loaded first");
  151. return false;
  152. }
  153. if (levelWidth != width_ || levelHeight != height_ || format != format_)
  154. {
  155. URHO3D_LOGERROR("Texture array layer does not match size or format of layer 0");
  156. return false;
  157. }
  158. }
  159. for (unsigned i = 0; i < levels_; ++i)
  160. {
  161. SetData(layer, i, 0, 0, levelWidth, levelHeight, levelData);
  162. memoryUse += levelWidth * levelHeight * components;
  163. if (i < levels_ - 1)
  164. {
  165. mipImage = image->GetNextLevel(); image = mipImage;
  166. levelData = image->GetData();
  167. levelWidth = image->GetWidth();
  168. levelHeight = image->GetHeight();
  169. }
  170. }
  171. }
  172. else
  173. {
  174. int width = image->GetWidth();
  175. int height = image->GetHeight();
  176. unsigned levels = image->GetNumCompressedLevels();
  177. unsigned format = graphics_->GetFormat(image->GetCompressedFormat());
  178. bool needDecompress = false;
  179. if (!format)
  180. {
  181. format = Graphics::GetRGBAFormat();
  182. needDecompress = true;
  183. }
  184. unsigned mipsToSkip = mipsToSkip_[quality];
  185. if (mipsToSkip >= levels)
  186. mipsToSkip = levels - 1;
  187. while (mipsToSkip && (width / (1 << mipsToSkip) < 4 || height / (1 << mipsToSkip) < 4))
  188. --mipsToSkip;
  189. width /= (1 << mipsToSkip);
  190. height /= (1 << mipsToSkip);
  191. // Create the texture array when layer 0 is being loaded, assume rest of the layers are same size & format
  192. if (!layer)
  193. {
  194. SetNumLevels(Max((levels - mipsToSkip), 1U));
  195. SetSize(0, width, height, format);
  196. }
  197. else
  198. {
  199. if (!object_.ptr_)
  200. {
  201. //URHO3D_LOGERROR("Texture array layer 0 must be loaded first");
  202. return false;
  203. }
  204. if (width != width_ || height != height_ || format != format_)
  205. {
  206. URHO3D_LOGERROR("Texture array layer does not match size or format of layer 0");
  207. return false;
  208. }
  209. }
  210. for (unsigned i = 0; i < levels_ && i < levels - mipsToSkip; ++i)
  211. {
  212. CompressedLevel level = image->GetCompressedLevel(i + mipsToSkip);
  213. if (!needDecompress)
  214. {
  215. SetData(layer, i, 0, 0, level.width_, level.height_, level.data_);
  216. memoryUse += level.rows_ * level.rowSize_;
  217. }
  218. else
  219. {
  220. unsigned char* rgbaData = new unsigned char[level.width_ * level.height_ * 4];
  221. level.Decompress(rgbaData);
  222. SetData(layer, i, 0, 0, level.width_, level.height_, rgbaData);
  223. memoryUse += level.width_ * level.height_ * 4;
  224. delete[] rgbaData;
  225. }
  226. }
  227. }
  228. layerMemoryUse_[layer] = memoryUse;
  229. unsigned totalMemoryUse = sizeof(Texture2DArray) + layerMemoryUse_.Capacity() * sizeof(unsigned);
  230. for (unsigned i = 0; i < layers_; ++i)
  231. totalMemoryUse += layerMemoryUse_[i];
  232. SetMemoryUse(totalMemoryUse);
  233. return true;
  234. }
  235. bool Texture2DArray::GetData(unsigned layer, unsigned level, void* dest) const
  236. {
  237. URHO3D_LOGERROR("Texture2DArray not supported on Direct3D9, can not get data");
  238. return false;
  239. }
  240. bool Texture2DArray::Create()
  241. {
  242. Release();
  243. if (!graphics_ || !width_ || !height_ || !layers_)
  244. return false;
  245. URHO3D_LOGERROR("Texture2DArray not supported on Direct3D9, can not create");
  246. return false;
  247. }
  248. }