D3D9Texture.cpp 8.1 KB

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