D3D9Texture.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "FileSystem.h"
  24. #include "Graphics.h"
  25. #include "GraphicsImpl.h"
  26. #include "Material.h"
  27. #include "Profiler.h"
  28. #include "ResourceCache.h"
  29. #include "StringUtils.h"
  30. #include "Texture.h"
  31. #include "XMLFile.h"
  32. #include "DebugNew.h"
  33. namespace Urho3D
  34. {
  35. static const String addressModeNames[] =
  36. {
  37. "wrap",
  38. "mirror",
  39. "clamp",
  40. "border",
  41. ""
  42. };
  43. static const String filterModeNames[] =
  44. {
  45. "nearest",
  46. "bilinear",
  47. "trilinear",
  48. "anisotropic",
  49. "default",
  50. ""
  51. };
  52. Texture::Texture(Context* context) :
  53. Resource(context),
  54. GPUObject(GetSubsystem<Graphics>()),
  55. format_(D3DFMT_UNKNOWN),
  56. pool_(D3DPOOL_MANAGED),
  57. usage_(0),
  58. levels_(0),
  59. requestedLevels_(0),
  60. width_(0),
  61. height_(0),
  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. bool Texture::IsCompressed() const
  96. {
  97. return format_ == D3DFMT_DXT1 || format_ == D3DFMT_DXT3 || format_ == D3DFMT_DXT5;
  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 (IsCompressed())
  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. case D3DFMT_R16F:
  140. return width * 2;
  141. case D3DFMT_X8R8G8B8:
  142. // Note: here source and destination data size differ
  143. return width * 3;
  144. case D3DFMT_A8R8G8B8:
  145. case D3DFMT_G16R16:
  146. case D3DFMT_R32F:
  147. case D3DFMT_G16R16F:
  148. case D3DFMT_D24S8:
  149. case D3DFMT_D32:
  150. return width * 4;
  151. case D3DFMT_A16B16G16R16:
  152. case D3DFMT_A16B16G16R16F:
  153. return width * 8;
  154. case D3DFMT_A32B32G32R32F:
  155. return width * 16;
  156. case D3DFMT_DXT1:
  157. return ((width + 3) >> 2) * 8;
  158. case D3DFMT_DXT3:
  159. case D3DFMT_DXT5:
  160. return ((width + 3) >> 2) * 16;
  161. default:
  162. return 0;
  163. }
  164. }
  165. void Texture::LoadParameters()
  166. {
  167. ResourceCache* cache = GetSubsystem<ResourceCache>();
  168. String xmlName = ReplaceExtension(GetName(), ".xml");
  169. if (cache->Exists(xmlName))
  170. {
  171. XMLFile* file = cache->GetResource<XMLFile>(xmlName);
  172. LoadParameters(file);
  173. }
  174. }
  175. void Texture::LoadParameters(XMLFile* file)
  176. {
  177. if (!file)
  178. return;
  179. XMLElement rootElem = file->GetRoot();
  180. LoadParameters(rootElem);
  181. }
  182. void Texture::LoadParameters(const XMLElement& element)
  183. {
  184. XMLElement paramElem = element.GetChild();
  185. while (paramElem)
  186. {
  187. String name = paramElem.GetName();
  188. if (name == "address")
  189. {
  190. String coord = paramElem.GetAttributeLower("coord");
  191. if (coord.Length() >= 1)
  192. {
  193. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  194. String mode = paramElem.GetAttributeLower("mode");
  195. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode, addressModeNames, ADDRESS_WRAP));
  196. }
  197. }
  198. if (name == "border")
  199. SetBorderColor(paramElem.GetColor("color"));
  200. if (name == "filter")
  201. {
  202. String mode = paramElem.GetAttributeLower("mode");
  203. SetFilterMode((TextureFilterMode)GetStringListIndex(mode, filterModeNames, FILTER_DEFAULT));
  204. }
  205. if (name == "mipmap")
  206. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  207. if (name == "quality")
  208. {
  209. if (paramElem.HasAttribute("low"))
  210. mipsToSkip_[QUALITY_LOW] = Max(paramElem.GetInt("low"), 0);
  211. if (paramElem.HasAttribute("med"))
  212. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("med"), 0);
  213. if (paramElem.HasAttribute("medium"))
  214. mipsToSkip_[QUALITY_MEDIUM] = Max(paramElem.GetInt("medium"), 0);
  215. if (paramElem.HasAttribute("high"))
  216. mipsToSkip_[QUALITY_HIGH] = Max(paramElem.GetInt("high"), 0);
  217. // Make sure a higher quality level does not actually skip more mips
  218. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  219. {
  220. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  221. mipsToSkip_[i] = mipsToSkip_[i - 1];
  222. }
  223. }
  224. paramElem = paramElem.GetNext();
  225. }
  226. }
  227. void Texture::CheckTextureBudget(ShortStringHash type)
  228. {
  229. ResourceCache* cache = GetSubsystem<ResourceCache>();
  230. unsigned textureBudget = cache->GetMemoryBudget(type);
  231. unsigned textureUse = cache->GetMemoryUse(type);
  232. if (!textureBudget)
  233. return;
  234. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  235. // Therefore free unused materials first
  236. if (textureUse > textureBudget)
  237. cache->ReleaseResources(Material::GetTypeStatic());
  238. }
  239. }