Texture.cpp 7.9 KB

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