Texture.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "Material.h"
  25. #include "Profiler.h"
  26. #include "Renderer.h"
  27. #include "RendererImpl.h"
  28. #include "ResourceCache.h"
  29. #include "StringUtils.h"
  30. #include "Texture.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. static const std::string addressModeNames[] =
  34. {
  35. "wrap",
  36. "mirror",
  37. "clamp",
  38. "border"
  39. };
  40. static const std::string filterModeNames[] =
  41. {
  42. "nearest",
  43. "bilinear",
  44. "trilinear",
  45. "anisotropic",
  46. "default"
  47. };
  48. int Texture::sQuality = QUALITY_HIGH;
  49. Texture::Texture(Renderer* renderer, const std::string& name) :
  50. Resource(name),
  51. GPUObject(renderer),
  52. mFormat(D3DFMT_UNKNOWN),
  53. mUsage(0),
  54. mLevels(0),
  55. mRequestedLevels(0),
  56. mWidth(0),
  57. mHeight(0),
  58. mDataLost(false),
  59. mFilterMode(FILTER_DEFAULT)
  60. {
  61. for (int i = 0; i < MAX_COORDS; ++i)
  62. mAddressMode[i] = ADDRESS_WRAP;
  63. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  64. mMipsToSkip[i] = MAX_TEXTURE_QUALITY_LEVELS - 1 - i;
  65. }
  66. Texture::~Texture()
  67. {
  68. }
  69. void Texture::setNumLevels(unsigned levels)
  70. {
  71. if (mUsage & D3DUSAGE_DEPTHSTENCIL)
  72. mRequestedLevels = 1;
  73. else
  74. mRequestedLevels = levels;
  75. }
  76. void Texture::setFilterMode(TextureFilterMode mode)
  77. {
  78. mFilterMode = mode;
  79. }
  80. void Texture::setAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  81. {
  82. mAddressMode[coord] = mode;
  83. }
  84. void Texture::setBorderColor(const Color& color)
  85. {
  86. mBorderColor = color;
  87. }
  88. void Texture::setBackupTexture(Texture* texture)
  89. {
  90. mBackupTexture = texture;
  91. }
  92. void Texture::clearDataLost()
  93. {
  94. mDataLost = false;
  95. }
  96. TextureUsage Texture::getUsage() const
  97. {
  98. if (mUsage & D3DUSAGE_DEPTHSTENCIL)
  99. return TEXTURE_DEPTHSTENCIL;
  100. if (mUsage & D3DUSAGE_RENDERTARGET)
  101. return TEXTURE_RENDERTARGET;
  102. if (mPool == D3DPOOL_DEFAULT)
  103. return TEXTURE_DYNAMIC;
  104. return TEXTURE_STATIC;
  105. }
  106. unsigned Texture::getCompressedD3DFormat(CompressedFormat format)
  107. {
  108. switch (format)
  109. {
  110. case CF_DXT1:
  111. return D3DFMT_DXT1;
  112. case CF_DXT3:
  113. return D3DFMT_DXT3;
  114. case CF_DXT5:
  115. return D3DFMT_DXT5;
  116. }
  117. return D3DFMT_UNKNOWN;
  118. }
  119. void Texture::loadParameters(ResourceCache* cache)
  120. {
  121. if (!cache)
  122. return;
  123. std::string texPath, texName, texExt;
  124. splitPath(getName(), texPath, texName, texExt);
  125. std::string xmlName = texPath + texName + ".xml";
  126. if (cache->exists(xmlName))
  127. {
  128. XMLFile* xml = cache->getResource<XMLFile>(xmlName);
  129. loadParameters(*xml);
  130. }
  131. }
  132. void Texture::loadParameters(XMLFile& xml)
  133. {
  134. XMLElement rootElem = xml.getRootElement();
  135. XMLElement paramElem = rootElem.getChildElement("");
  136. while (paramElem)
  137. {
  138. std::string name = paramElem.getName();
  139. if (name == "address")
  140. {
  141. std::string coord = paramElem.getStringLower("coord");
  142. if (coord.length() >= 1)
  143. {
  144. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  145. std::string mode = paramElem.getStringLower("mode");
  146. setAddressMode(coordIndex, (TextureAddressMode)getIndexFromStringList(mode, addressModeNames, MAX_ADDRESSMODES,
  147. ADDRESS_WRAP));
  148. }
  149. }
  150. if (name == "border")
  151. setBorderColor(paramElem.getColor("color"));
  152. if (name == "filter")
  153. {
  154. std::string mode = paramElem.getStringLower("mode");
  155. setFilterMode((TextureFilterMode)getIndexFromStringList(mode, filterModeNames, MAX_FILTERMODES, FILTER_DEFAULT));
  156. }
  157. if (name == "mipmap")
  158. setNumLevels(paramElem.getBool("enable") ? 0 : 1);
  159. if (name == "quality")
  160. {
  161. if (paramElem.hasAttribute("low"))
  162. mMipsToSkip[QUALITY_LOW] = max(paramElem.getInt("low"), 0);
  163. if (paramElem.hasAttribute("med"))
  164. mMipsToSkip[QUALITY_MEDIUM] = max(paramElem.getInt("med"), 0);
  165. if (paramElem.hasAttribute("medium"))
  166. mMipsToSkip[QUALITY_MEDIUM] = max(paramElem.getInt("medium"), 0);
  167. if (paramElem.hasAttribute("high"))
  168. mMipsToSkip[QUALITY_HIGH] = max(paramElem.getInt("high"), 0);
  169. // Make sure a higher quality level does not actually skip more mips
  170. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  171. {
  172. if (mMipsToSkip[i] > mMipsToSkip[i - 1])
  173. mMipsToSkip[i] = mMipsToSkip[i - 1];
  174. }
  175. }
  176. paramElem = paramElem.getNextElement();
  177. }
  178. }
  179. void Texture::setQuality(int quality)
  180. {
  181. sQuality = clamp(quality, 0, MAX_TEXTURE_QUALITY_LEVELS - 1);
  182. }
  183. void Texture::checkTextureBudget(ShortStringHash type, ResourceCache* cache)
  184. {
  185. if (!cache)
  186. return;
  187. unsigned textureBudget = cache->getMemoryBudget(type);
  188. unsigned textureUse = cache->getMemoryUse(type);
  189. if (!textureBudget)
  190. return;
  191. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  192. // Therefore free unused materials first
  193. if (textureUse > textureBudget)
  194. cache->releaseResources(Material::getTypeStatic());
  195. }