D3D9Texture.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. bool Texture::IsCompressed() const
  100. {
  101. return format_ == D3DFMT_DXT1 || format_ == D3DFMT_DXT3 || format_ == D3DFMT_DXT5;
  102. }
  103. int Texture::GetLevelWidth(unsigned level) const
  104. {
  105. if (level > levels_)
  106. return 0;
  107. return Max(width_ >> level, 1);
  108. }
  109. int Texture::GetLevelHeight(unsigned level) const
  110. {
  111. if (level > levels_)
  112. return 0;
  113. return Max(height_ >> level, 1);
  114. }
  115. TextureUsage Texture::GetUsage() const
  116. {
  117. if (usage_ & D3DUSAGE_DEPTHSTENCIL)
  118. return TEXTURE_DEPTHSTENCIL;
  119. if (usage_ & D3DUSAGE_RENDERTARGET)
  120. return TEXTURE_RENDERTARGET;
  121. if (pool_ == D3DPOOL_DEFAULT)
  122. return TEXTURE_DYNAMIC;
  123. return TEXTURE_STATIC;
  124. }
  125. unsigned Texture::GetDataSize(int width, int height) const
  126. {
  127. if (IsCompressed())
  128. return GetRowDataSize(width) * ((height + 3) >> 2);
  129. else
  130. return GetRowDataSize(width) * height;
  131. }
  132. unsigned Texture::GetRowDataSize(int width) const
  133. {
  134. switch (format_)
  135. {
  136. case D3DFMT_A8:
  137. case D3DFMT_L8:
  138. return width;
  139. case D3DFMT_D16:
  140. case D3DFMT_R5G6B5:
  141. case D3DFMT_A4R4G4B4:
  142. case D3DFMT_A8L8:
  143. return width * 2;
  144. case D3DFMT_X8R8G8B8:
  145. // Note: here source and destination data size differ
  146. return width * 3;
  147. case D3DFMT_A8R8G8B8:
  148. case D3DFMT_R32F:
  149. case D3DFMT_D24S8:
  150. return width * 4;
  151. case D3DFMT_DXT1:
  152. return ((width + 3) >> 2) * 8;
  153. case D3DFMT_DXT3:
  154. case D3DFMT_DXT5:
  155. return ((width + 3) >> 2) * 16;
  156. default:
  157. return 0;
  158. }
  159. }
  160. unsigned Texture::GetDXTFormat(CompressedFormat format)
  161. {
  162. switch (format)
  163. {
  164. case CF_DXT1:
  165. return D3DFMT_DXT1;
  166. case CF_DXT3:
  167. return D3DFMT_DXT3;
  168. case CF_DXT5:
  169. return D3DFMT_DXT5;
  170. }
  171. return 0;
  172. }
  173. void Texture::LoadParameters()
  174. {
  175. ResourceCache* cache = GetSubsystem<ResourceCache>();
  176. String texPath, texName, texExt;
  177. SplitPath(GetName(), texPath, texName, texExt);
  178. String xmlName = texPath + texName + ".xml";
  179. if (cache->Exists(xmlName))
  180. {
  181. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  182. LoadParameters(file);
  183. }
  184. }
  185. void Texture::LoadParameters(XMLFile* file)
  186. {
  187. if (!file)
  188. return;
  189. XMLElement rootElem = file->GetRoot();
  190. LoadParameters(rootElem);
  191. }
  192. void Texture::LoadParameters(const XMLElement& elem)
  193. {
  194. XMLElement paramElem = elem.GetChild();
  195. while (paramElem)
  196. {
  197. String name = paramElem.GetName();
  198. if (name == "address")
  199. {
  200. String coord = paramElem.GetAttributeLower("coord");
  201. if (coord.Length() >= 1)
  202. {
  203. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  204. String mode = paramElem.GetAttributeLower("mode");
  205. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  206. }
  207. }
  208. if (name == "border")
  209. SetBorderColor(paramElem.GetColor("color"));
  210. if (name == "filter")
  211. {
  212. String mode = paramElem.GetAttributeLower("mode");
  213. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  214. }
  215. if (name == "mipmap")
  216. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  217. if (name == "quality")
  218. {
  219. if (paramElem.HasAttribute("low"))
  220. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  221. if (paramElem.HasAttribute("med"))
  222. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  223. if (paramElem.HasAttribute("medium"))
  224. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  225. if (paramElem.HasAttribute("high"))
  226. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  227. // Make sure a higher quality level does not actually skip more mips
  228. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  229. {
  230. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  231. mipsToSkip_[i] = mipsToSkip_[i - 1];
  232. }
  233. }
  234. paramElem = paramElem.GetNext();
  235. }
  236. }
  237. void Texture::CheckTextureBudget(ShortStringHash type)
  238. {
  239. ResourceCache* cache = GetSubsystem<ResourceCache>();
  240. unsigned textureBudget = cache->GetMemoryBudget(type);
  241. unsigned textureUse = cache->GetMemoryUse(type);
  242. if (!textureBudget)
  243. return;
  244. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  245. // Therefore free unused materials first
  246. if (textureUse > textureBudget)
  247. cache->ReleaseResources(Material::GetTypeStatic());
  248. }