Texture.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "../Graphics/Graphics.h"
  24. #include "../Graphics/GraphicsImpl.h"
  25. #include "../Graphics/Material.h"
  26. #include "../IO/FileSystem.h"
  27. #include "../IO/Log.h"
  28. #include "../Resource/ResourceCache.h"
  29. #include "../Resource/XMLFile.h"
  30. #include "../DebugNew.h"
  31. namespace Urho3D
  32. {
  33. static const char* addressModeNames[] =
  34. {
  35. "wrap",
  36. "mirror",
  37. "clamp",
  38. "border",
  39. nullptr
  40. };
  41. static const char* filterModeNames[] =
  42. {
  43. "nearest",
  44. "bilinear",
  45. "trilinear",
  46. "anisotropic",
  47. "nearestanisotropic",
  48. "default",
  49. nullptr
  50. };
  51. Texture::Texture(Context* context) :
  52. ResourceWithMetadata(context),
  53. GPUObject(GetSubsystem<Graphics>())
  54. {
  55. }
  56. Texture::~Texture() = default;
  57. void Texture::SetNumLevels(unsigned levels)
  58. {
  59. if (usage_ > TEXTURE_RENDERTARGET)
  60. requestedLevels_ = 1;
  61. else
  62. requestedLevels_ = levels;
  63. }
  64. void Texture::SetFilterMode(TextureFilterMode mode)
  65. {
  66. filterMode_ = mode;
  67. parametersDirty_ = true;
  68. }
  69. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  70. {
  71. addressModes_[coord] = mode;
  72. parametersDirty_ = true;
  73. }
  74. void Texture::SetAnisotropy(unsigned level)
  75. {
  76. anisotropy_ = level;
  77. parametersDirty_ = true;
  78. }
  79. void Texture::SetShadowCompare(bool enable)
  80. {
  81. shadowCompare_ = enable;
  82. parametersDirty_ = true;
  83. }
  84. void Texture::SetBorderColor(const Color& color)
  85. {
  86. borderColor_ = color;
  87. parametersDirty_ = true;
  88. }
  89. void Texture::SetBackupTexture(Texture* texture)
  90. {
  91. backupTexture_ = texture;
  92. }
  93. void Texture::SetMipsToSkip(MaterialQuality quality, int toSkip)
  94. {
  95. if (quality >= QUALITY_LOW && quality < MAX_TEXTURE_QUALITY_LEVELS)
  96. {
  97. mipsToSkip_[quality] = (unsigned)toSkip;
  98. // Make sure a higher quality level does not actually skip more mips
  99. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  100. {
  101. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  102. mipsToSkip_[i] = mipsToSkip_[i - 1];
  103. }
  104. }
  105. }
  106. int Texture::GetMipsToSkip(MaterialQuality quality) const
  107. {
  108. return (quality >= QUALITY_LOW && quality < MAX_TEXTURE_QUALITY_LEVELS) ? mipsToSkip_[quality] : 0;
  109. }
  110. int Texture::GetLevelWidth(unsigned level) const
  111. {
  112. if (level > levels_)
  113. return 0;
  114. return Max(width_ >> level, 1);
  115. }
  116. int Texture::GetLevelHeight(unsigned level) const
  117. {
  118. if (level > levels_)
  119. return 0;
  120. return Max(height_ >> level, 1);
  121. }
  122. int Texture::GetLevelDepth(unsigned level) const
  123. {
  124. if (level > levels_)
  125. return 0;
  126. return Max(depth_ >> level, 1);
  127. }
  128. unsigned Texture::GetDataSize(int width, int height) const
  129. {
  130. if (IsCompressed())
  131. return GetRowDataSize(width) * ((height + 3) >> 2u);
  132. else
  133. return GetRowDataSize(width) * height;
  134. }
  135. unsigned Texture::GetDataSize(int width, int height, int depth) const
  136. {
  137. return depth * GetDataSize(width, height);
  138. }
  139. unsigned Texture::GetComponents() const
  140. {
  141. if (!width_ || IsCompressed())
  142. return 0;
  143. else
  144. return GetRowDataSize(width_) / width_;
  145. }
  146. void Texture::SetParameters(XMLFile* file)
  147. {
  148. if (!file)
  149. return;
  150. XMLElement rootElem = file->GetRoot();
  151. SetParameters(rootElem);
  152. }
  153. void Texture::SetParameters(const XMLElement& element)
  154. {
  155. LoadMetadataFromXML(element);
  156. for (XMLElement paramElem = element.GetChild(); paramElem; paramElem = paramElem.GetNext())
  157. {
  158. String name = paramElem.GetName();
  159. if (name == "address")
  160. {
  161. String coord = paramElem.GetAttributeLower("coord");
  162. if (coord.Length() >= 1)
  163. {
  164. auto coordIndex = (TextureCoordinate)(coord[0] - 'u');
  165. String mode = paramElem.GetAttributeLower("mode");
  166. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode.CString(), addressModeNames, ADDRESS_WRAP));
  167. }
  168. }
  169. if (name == "border")
  170. SetBorderColor(paramElem.GetColor("color"));
  171. if (name == "filter")
  172. {
  173. String mode = paramElem.GetAttributeLower("mode");
  174. SetFilterMode((TextureFilterMode)GetStringListIndex(mode.CString(), filterModeNames, FILTER_DEFAULT));
  175. if (paramElem.HasAttribute("anisotropy"))
  176. SetAnisotropy(paramElem.GetUInt("anisotropy"));
  177. }
  178. if (name == "mipmap")
  179. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  180. if (name == "quality")
  181. {
  182. if (paramElem.HasAttribute("low"))
  183. SetMipsToSkip(QUALITY_LOW, paramElem.GetInt("low"));
  184. if (paramElem.HasAttribute("med"))
  185. SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("med"));
  186. if (paramElem.HasAttribute("medium"))
  187. SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("medium"));
  188. if (paramElem.HasAttribute("high"))
  189. SetMipsToSkip(QUALITY_HIGH, paramElem.GetInt("high"));
  190. }
  191. if (name == "srgb")
  192. SetSRGB(paramElem.GetBool("enable"));
  193. }
  194. }
  195. void Texture::SetParametersDirty()
  196. {
  197. parametersDirty_ = true;
  198. }
  199. void Texture::SetLevelsDirty()
  200. {
  201. if (usage_ == TEXTURE_RENDERTARGET && levels_ > 1)
  202. levelsDirty_ = true;
  203. }
  204. unsigned Texture::CheckMaxLevels(int width, int height, unsigned requestedLevels)
  205. {
  206. unsigned maxLevels = 1;
  207. while (width > 1 || height > 1)
  208. {
  209. ++maxLevels;
  210. width = width > 1 ? (width >> 1u) : 1;
  211. height = height > 1 ? (height >> 1u) : 1;
  212. }
  213. if (!requestedLevels || maxLevels < requestedLevels)
  214. return maxLevels;
  215. else
  216. return requestedLevels;
  217. }
  218. unsigned Texture::CheckMaxLevels(int width, int height, int depth, unsigned requestedLevels)
  219. {
  220. unsigned maxLevels = 1;
  221. while (width > 1 || height > 1 || depth > 1)
  222. {
  223. ++maxLevels;
  224. width = width > 1 ? (width >> 1u) : 1;
  225. height = height > 1 ? (height >> 1u) : 1;
  226. depth = depth > 1 ? (depth >> 1u) : 1;
  227. }
  228. if (!requestedLevels || maxLevels < requestedLevels)
  229. return maxLevels;
  230. else
  231. return requestedLevels;
  232. }
  233. void Texture::CheckTextureBudget(StringHash type)
  234. {
  235. auto* cache = GetSubsystem<ResourceCache>();
  236. unsigned long long textureBudget = cache->GetMemoryBudget(type);
  237. unsigned long long textureUse = cache->GetMemoryUse(type);
  238. if (!textureBudget)
  239. return;
  240. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  241. // Therefore free unused materials first
  242. if (textureUse > textureBudget)
  243. cache->ReleaseResources(Material::GetTypeStatic());
  244. }
  245. }