D3D9Texture.cpp 8.1 KB

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