D3D9Texture.cpp 7.6 KB

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