D3D9Texture.cpp 8.3 KB

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