D3D9Texture.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. namespace Urho3D
  35. {
  36. static const String addressModeNames[] =
  37. {
  38. "wrap",
  39. "mirror",
  40. "clamp",
  41. "border",
  42. ""
  43. };
  44. static const String filterModeNames[] =
  45. {
  46. "nearest",
  47. "bilinear",
  48. "trilinear",
  49. "anisotropic",
  50. "default",
  51. ""
  52. };
  53. Texture::Texture(Context* context) :
  54. Resource(context),
  55. GPUObject(GetSubsystem<Graphics>()),
  56. format_(D3DFMT_UNKNOWN),
  57. pool_(D3DPOOL_MANAGED),
  58. usage_(0),
  59. levels_(0),
  60. requestedLevels_(0),
  61. width_(0),
  62. height_(0),
  63. filterMode_(FILTER_DEFAULT)
  64. {
  65. for (int i = 0; i < MAX_COORDS; ++i)
  66. addressMode_[i] = ADDRESS_WRAP;
  67. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  68. mipsToSkip_[i] = MAX_TEXTURE_QUALITY_LEVELS - 1 - i;
  69. }
  70. Texture::~Texture()
  71. {
  72. }
  73. void Texture::SetNumLevels(unsigned levels)
  74. {
  75. if (usage_ & D3DUSAGE_DEPTHSTENCIL)
  76. requestedLevels_ = 1;
  77. else
  78. requestedLevels_ = levels;
  79. }
  80. void Texture::SetFilterMode(TextureFilterMode mode)
  81. {
  82. filterMode_ = mode;
  83. }
  84. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  85. {
  86. addressMode_[coord] = mode;
  87. }
  88. void Texture::SetBorderColor(const Color& color)
  89. {
  90. borderColor_ = color;
  91. }
  92. void Texture::SetBackupTexture(Texture* texture)
  93. {
  94. backupTexture_ = texture;
  95. }
  96. bool Texture::IsCompressed() const
  97. {
  98. return format_ == D3DFMT_DXT1 || format_ == D3DFMT_DXT3 || format_ == D3DFMT_DXT5;
  99. }
  100. int Texture::GetLevelWidth(unsigned level) const
  101. {
  102. if (level > levels_)
  103. return 0;
  104. return Max(width_ >> level, 1);
  105. }
  106. int Texture::GetLevelHeight(unsigned level) const
  107. {
  108. if (level > levels_)
  109. return 0;
  110. return Max(height_ >> level, 1);
  111. }
  112. TextureUsage Texture::GetUsage() const
  113. {
  114. if (usage_ & D3DUSAGE_DEPTHSTENCIL)
  115. return TEXTURE_DEPTHSTENCIL;
  116. if (usage_ & D3DUSAGE_RENDERTARGET)
  117. return TEXTURE_RENDERTARGET;
  118. if (pool_ == D3DPOOL_DEFAULT)
  119. return TEXTURE_DYNAMIC;
  120. return TEXTURE_STATIC;
  121. }
  122. unsigned Texture::GetDataSize(int width, int height) const
  123. {
  124. if (IsCompressed())
  125. return GetRowDataSize(width) * ((height + 3) >> 2);
  126. else
  127. return GetRowDataSize(width) * height;
  128. }
  129. unsigned Texture::GetRowDataSize(int width) const
  130. {
  131. switch (format_)
  132. {
  133. case D3DFMT_A8:
  134. case D3DFMT_L8:
  135. return width;
  136. case D3DFMT_D16:
  137. case D3DFMT_R5G6B5:
  138. case D3DFMT_A4R4G4B4:
  139. case D3DFMT_A8L8:
  140. case D3DFMT_R16F:
  141. return width * 2;
  142. case D3DFMT_X8R8G8B8:
  143. // Note: here source and destination data size differ
  144. return width * 3;
  145. case D3DFMT_A8R8G8B8:
  146. case D3DFMT_G16R16:
  147. case D3DFMT_R32F:
  148. case D3DFMT_G16R16F:
  149. case D3DFMT_D24S8:
  150. case D3DFMT_D32:
  151. return width * 4;
  152. case D3DFMT_A16B16G16R16:
  153. case D3DFMT_A16B16G16R16F:
  154. return width * 8;
  155. case D3DFMT_A32B32G32R32F:
  156. return width * 16;
  157. case D3DFMT_DXT1:
  158. return ((width + 3) >> 2) * 8;
  159. case D3DFMT_DXT3:
  160. case D3DFMT_DXT5:
  161. return ((width + 3) >> 2) * 16;
  162. default:
  163. return 0;
  164. }
  165. }
  166. void Texture::LoadParameters()
  167. {
  168. ResourceCache* cache = GetSubsystem<ResourceCache>();
  169. String xmlName = ReplaceExtension(GetName(), ".xml");
  170. if (cache->Exists(xmlName))
  171. {
  172. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  173. LoadParameters(file);
  174. }
  175. }
  176. void Texture::LoadParameters(XMLFile* file)
  177. {
  178. if (!file)
  179. return;
  180. XMLElement rootElem = file->GetRoot();
  181. LoadParameters(rootElem);
  182. }
  183. void Texture::LoadParameters(const XMLElement& element)
  184. {
  185. XMLElement paramElem = element.GetChild();
  186. while (paramElem)
  187. {
  188. String name = paramElem.GetName();
  189. if (name == "address")
  190. {
  191. String coord = paramElem.GetAttributeLower("coord");
  192. if (coord.Length() >= 1)
  193. {
  194. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  195. String mode = paramElem.GetAttributeLower("mode");
  196. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  197. }
  198. }
  199. if (name == "border")
  200. SetBorderColor(paramElem.GetColor("color"));
  201. if (name == "filter")
  202. {
  203. String mode = paramElem.GetAttributeLower("mode");
  204. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  205. }
  206. if (name == "mipmap")
  207. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  208. if (name == "quality")
  209. {
  210. if (paramElem.HasAttribute("low"))
  211. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  212. if (paramElem.HasAttribute("med"))
  213. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  214. if (paramElem.HasAttribute("medium"))
  215. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  216. if (paramElem.HasAttribute("high"))
  217. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  218. // Make sure a higher quality level does not actually skip more mips
  219. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  220. {
  221. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  222. mipsToSkip_[i] = mipsToSkip_[i - 1];
  223. }
  224. }
  225. paramElem = paramElem.GetNext();
  226. }
  227. }
  228. void Texture::CheckTextureBudget(ShortStringHash type)
  229. {
  230. ResourceCache* cache = GetSubsystem<ResourceCache>();
  231. unsigned textureBudget = cache->GetMemoryBudget(type);
  232. unsigned textureUse = cache->GetMemoryUse(type);
  233. if (!textureBudget)
  234. return;
  235. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  236. // Therefore free unused materials first
  237. if (textureUse > textureBudget)
  238. cache->ReleaseResources(Material::GetTypeStatic());
  239. }
  240. }