Texture.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. static const String filterModeNames[] =
  42. {
  43. "nearest",
  44. "bilinear",
  45. "trilinear",
  46. "anisotropic",
  47. "default"
  48. };
  49. Texture::Texture(Context* context) :
  50. Resource(context),
  51. GPUObject(GetSubsystem<Graphics>()),
  52. format_(D3DFMT_UNKNOWN),
  53. pool_(D3DPOOL_MANAGED),
  54. usage_(0),
  55. levels_(0),
  56. requestedLevels_(0),
  57. width_(0),
  58. height_(0),
  59. dataLost_(false),
  60. filterMode_(FILTER_DEFAULT)
  61. {
  62. for (int i = 0; i < MAX_COORDS; ++i)
  63. addressMode_[i] = ADDRESS_WRAP;
  64. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  65. mipsToSkip_[i] = MAX_TEXTURE_QUALITY_LEVELS - 1 - i;
  66. }
  67. Texture::~Texture()
  68. {
  69. }
  70. void Texture::SetNumLevels(unsigned levels)
  71. {
  72. if (usage_ & D3DUSAGE_DEPTHSTENCIL)
  73. requestedLevels_ = 1;
  74. else
  75. requestedLevels_ = levels;
  76. }
  77. void Texture::SetFilterMode(TextureFilterMode mode)
  78. {
  79. filterMode_ = mode;
  80. }
  81. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  82. {
  83. addressMode_[coord] = mode;
  84. }
  85. void Texture::SetBorderColor(const Color& color)
  86. {
  87. borderColor_ = color;
  88. }
  89. void Texture::SetBackupTexture(Texture* texture)
  90. {
  91. backupTexture_ = texture;
  92. }
  93. void Texture::ClearDataLost()
  94. {
  95. dataLost_ = false;
  96. }
  97. TextureUsage Texture::GetUsage() const
  98. {
  99. if (usage_ & D3DUSAGE_DEPTHSTENCIL)
  100. return TEXTURE_DEPTHSTENCIL;
  101. if (usage_ & D3DUSAGE_RENDERTARGET)
  102. return TEXTURE_RENDERTARGET;
  103. if (pool_ == D3DPOOL_DEFAULT)
  104. return TEXTURE_DYNAMIC;
  105. return TEXTURE_STATIC;
  106. }
  107. unsigned Texture::GetCompressedD3DFormat(CompressedFormat format)
  108. {
  109. switch (format)
  110. {
  111. case CF_DXT1:
  112. return D3DFMT_DXT1;
  113. case CF_DXT3:
  114. return D3DFMT_DXT3;
  115. case CF_DXT5:
  116. return D3DFMT_DXT5;
  117. }
  118. return D3DFMT_UNKNOWN;
  119. }
  120. void Texture::LoadParameters()
  121. {
  122. ResourceCache* cache = GetSubsystem<ResourceCache>();
  123. String texPath, texName, texExt;
  124. SplitPath(GetName(), texPath, texName, texExt);
  125. String xmlName = texPath + texName + ".xml";
  126. if (cache->Exists(xmlName))
  127. {
  128. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  129. LoadParameters(file);
  130. }
  131. }
  132. void Texture::LoadParameters(XMLFile* file)
  133. {
  134. if (!file)
  135. return;
  136. XMLElement rootElem = file->GetRootElement();
  137. XMLElement paramElem = rootElem.GetChildElement("");
  138. while (paramElem)
  139. {
  140. String name = paramElem.GetName();
  141. if (name == "address")
  142. {
  143. String coord = paramElem.GetStringLower("coord");
  144. if (coord.Length() >= 1)
  145. {
  146. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  147. String mode = paramElem.GetStringLower("mode");
  148. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, MAX_ADDRESSMODES,
  149. ADDRESS_WRAP));
  150. }
  151. }
  152. if (name == "border")
  153. SetBorderColor(paramElem.GetColor("color"));
  154. if (name == "filter")
  155. {
  156. String mode = paramElem.GetStringLower("mode");
  157. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, MAX_FILTERMODES, FILTER_DEFAULT));
  158. }
  159. if (name == "mipmap")
  160. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  161. if (name == "quality")
  162. {
  163. if (paramElem.HasAttribute("low"))
  164. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  165. if (paramElem.HasAttribute("med"))
  166. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  167. if (paramElem.HasAttribute("medium"))
  168. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  169. if (paramElem.HasAttribute("high"))
  170. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  171. // Make sure a higher quality level does not actually skip more mips
  172. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  173. {
  174. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  175. mipsToSkip_[i] = mipsToSkip_[i - 1];
  176. }
  177. }
  178. paramElem = paramElem.GetNextElement();
  179. }
  180. }
  181. void Texture::CheckTextureBudget(ShortStringHash type)
  182. {
  183. ResourceCache* cache = GetSubsystem<ResourceCache>();
  184. unsigned textureBudget = cache->GetMemoryBudget(type);
  185. unsigned textureUse = cache->GetMemoryUse(type);
  186. if (!textureBudget)
  187. return;
  188. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  189. // Therefore free unused materials first
  190. if (textureUse > textureBudget)
  191. cache->ReleaseResources(Material::GetTypeStatic());
  192. }