Texture.cpp 7.7 KB

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