Texture.cpp 8.2 KB

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