Texture.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "FileSystem.h"
  25. #include "Graphics.h"
  26. #include "GraphicsImpl.h"
  27. #include "Material.h"
  28. #include "Profiler.h"
  29. #include "ResourceCache.h"
  30. #include "StringUtils.h"
  31. #include "Texture.h"
  32. #include "XMLFile.h"
  33. #include "DebugNew.h"
  34. static const String addressModeNames[] =
  35. {
  36. "wrap",
  37. "mirror",
  38. "clamp",
  39. "border",
  40. ""
  41. };
  42. static const String filterModeNames[] =
  43. {
  44. "nearest",
  45. "bilinear",
  46. "trilinear",
  47. "anisotropic",
  48. "default",
  49. ""
  50. };
  51. Texture::Texture(Context* context) :
  52. Resource(context),
  53. GPUObject(GetSubsystem<Graphics>()),
  54. format_(D3DFMT_UNKNOWN),
  55. pool_(D3DPOOL_MANAGED),
  56. usage_(0),
  57. levels_(0),
  58. requestedLevels_(0),
  59. width_(0),
  60. height_(0),
  61. dataLost_(false),
  62. filterMode_(FILTER_DEFAULT)
  63. {
  64. for (int i = 0; i < MAX_COORDS; ++i)
  65. addressMode_[i] = ADDRESS_WRAP;
  66. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  67. mipsToSkip_[i] = MAX_TEXTURE_QUALITY_LEVELS - 1 - i;
  68. }
  69. Texture::~Texture()
  70. {
  71. }
  72. void Texture::SetNumLevels(unsigned levels)
  73. {
  74. if (usage_ & D3DUSAGE_DEPTHSTENCIL)
  75. requestedLevels_ = 1;
  76. else
  77. requestedLevels_ = levels;
  78. }
  79. void Texture::SetFilterMode(TextureFilterMode mode)
  80. {
  81. filterMode_ = mode;
  82. }
  83. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  84. {
  85. addressMode_[coord] = mode;
  86. }
  87. void Texture::SetBorderColor(const Color& color)
  88. {
  89. borderColor_ = color;
  90. }
  91. void Texture::SetBackupTexture(Texture* texture)
  92. {
  93. backupTexture_ = texture;
  94. }
  95. void Texture::ClearDataLost()
  96. {
  97. dataLost_ = false;
  98. }
  99. TextureUsage Texture::GetUsage() const
  100. {
  101. if (usage_ & D3DUSAGE_DEPTHSTENCIL)
  102. return TEXTURE_DEPTHSTENCIL;
  103. if (usage_ & D3DUSAGE_RENDERTARGET)
  104. return TEXTURE_RENDERTARGET;
  105. if (pool_ == D3DPOOL_DEFAULT)
  106. return TEXTURE_DYNAMIC;
  107. return TEXTURE_STATIC;
  108. }
  109. unsigned Texture::GetCompressedD3DFormat(CompressedFormat format)
  110. {
  111. switch (format)
  112. {
  113. case CF_DXT1:
  114. return D3DFMT_DXT1;
  115. case CF_DXT3:
  116. return D3DFMT_DXT3;
  117. case CF_DXT5:
  118. return D3DFMT_DXT5;
  119. }
  120. return D3DFMT_UNKNOWN;
  121. }
  122. void Texture::LoadParameters()
  123. {
  124. ResourceCache* cache = GetSubsystem<ResourceCache>();
  125. String texPath, texName, texExt;
  126. SplitPath(GetName(), texPath, texName, texExt);
  127. String xmlName = texPath + texName + ".xml";
  128. if (cache->Exists(xmlName))
  129. {
  130. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  131. LoadParameters(file);
  132. }
  133. }
  134. void Texture::LoadParameters(XMLFile* file)
  135. {
  136. if (!file)
  137. return;
  138. XMLElement rootElem = file->GetRootElement();
  139. XMLElement paramElem = rootElem.GetChildElement("");
  140. while (paramElem)
  141. {
  142. String name = paramElem.GetName();
  143. if (name == "address")
  144. {
  145. String coord = paramElem.GetStringLower("coord");
  146. if (coord.Length() >= 1)
  147. {
  148. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  149. String mode = paramElem.GetStringLower("mode");
  150. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  151. }
  152. }
  153. if (name == "border")
  154. SetBorderColor(paramElem.GetColor("color"));
  155. if (name == "filter")
  156. {
  157. String mode = paramElem.GetStringLower("mode");
  158. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  159. }
  160. if (name == "mipmap")
  161. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  162. if (name == "quality")
  163. {
  164. if (paramElem.HasAttribute("low"))
  165. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  166. if (paramElem.HasAttribute("med"))
  167. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  168. if (paramElem.HasAttribute("medium"))
  169. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  170. if (paramElem.HasAttribute("high"))
  171. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  172. // Make sure a higher quality level does not actually skip more mips
  173. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  174. {
  175. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  176. mipsToSkip_[i] = mipsToSkip_[i - 1];
  177. }
  178. }
  179. paramElem = paramElem.GetNextElement();
  180. }
  181. }
  182. void Texture::CheckTextureBudget(ShortStringHash type)
  183. {
  184. ResourceCache* cache = GetSubsystem<ResourceCache>();
  185. unsigned textureBudget = cache->GetMemoryBudget(type);
  186. unsigned textureUse = cache->GetMemoryUse(type);
  187. if (!textureBudget)
  188. return;
  189. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  190. // Therefore free unused materials first
  191. if (textureUse > textureBudget)
  192. cache->ReleaseResources(Material::GetTypeStatic());
  193. }