D3D11Texture.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 "../../IO/FileSystem.h"
  23. #include "../../IO/Log.h"
  24. #include "../../Graphics/Graphics.h"
  25. #include "../../Graphics/GraphicsImpl.h"
  26. #include "../../Graphics/Material.h"
  27. #include "../../Core/Profiler.h"
  28. #include "../../Resource/ResourceCache.h"
  29. #include "../../Core/StringUtils.h"
  30. #include "../../Graphics/Texture.h"
  31. #include "../../Resource/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. static const D3D11_FILTER d3dFilterMode[] =
  53. {
  54. D3D11_FILTER_MIN_MAG_MIP_POINT,
  55. D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT,
  56. D3D11_FILTER_MIN_MAG_MIP_LINEAR,
  57. D3D11_FILTER_ANISOTROPIC,
  58. D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT,
  59. D3D11_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT,
  60. D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR,
  61. D3D11_FILTER_COMPARISON_ANISOTROPIC
  62. };
  63. static const D3D11_TEXTURE_ADDRESS_MODE d3dAddressMode[] =
  64. {
  65. D3D11_TEXTURE_ADDRESS_WRAP,
  66. D3D11_TEXTURE_ADDRESS_MIRROR,
  67. D3D11_TEXTURE_ADDRESS_CLAMP,
  68. D3D11_TEXTURE_ADDRESS_BORDER
  69. };
  70. Texture::Texture(Context* context) :
  71. Resource(context),
  72. GPUObject(GetSubsystem<Graphics>()),
  73. shaderResourceView_(0),
  74. sampler_(0),
  75. format_(DXGI_FORMAT_UNKNOWN),
  76. usage_(TEXTURE_STATIC),
  77. levels_(0),
  78. requestedLevels_(0),
  79. width_(0),
  80. height_(0),
  81. depth_(0),
  82. shadowCompare_(false),
  83. filterMode_(FILTER_DEFAULT),
  84. sRGB_(false),
  85. parametersDirty_(true)
  86. {
  87. for (int i = 0; i < MAX_COORDS; ++i)
  88. addressMode_[i] = ADDRESS_WRAP;
  89. for (int i = 0; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  90. mipsToSkip_[i] = MAX_TEXTURE_QUALITY_LEVELS - 1 - i;
  91. }
  92. Texture::~Texture()
  93. {
  94. }
  95. void Texture::SetNumLevels(unsigned levels)
  96. {
  97. if (usage_ > TEXTURE_RENDERTARGET)
  98. requestedLevels_ = 1;
  99. else
  100. requestedLevels_ = levels;
  101. }
  102. void Texture::SetFilterMode(TextureFilterMode mode)
  103. {
  104. filterMode_ = mode;
  105. parametersDirty_ = true;
  106. }
  107. void Texture::SetAddressMode(TextureCoordinate coord, TextureAddressMode mode)
  108. {
  109. addressMode_[coord] = mode;
  110. parametersDirty_ = true;
  111. }
  112. void Texture::SetShadowCompare(bool enable)
  113. {
  114. shadowCompare_ = enable;
  115. parametersDirty_ = true;
  116. }
  117. void Texture::SetBorderColor(const Color& color)
  118. {
  119. borderColor_ = color;
  120. parametersDirty_ = true;
  121. }
  122. void Texture::SetSRGB(bool enable)
  123. {
  124. if (graphics_)
  125. enable &= graphics_->GetSRGBSupport();
  126. // Note: on D3D11 sRGB only affects the texture before creation
  127. sRGB_ = enable;
  128. }
  129. void Texture::SetBackupTexture(Texture* texture)
  130. {
  131. backupTexture_ = texture;
  132. }
  133. void Texture::SetMipsToSkip(int quality, int mips)
  134. {
  135. if (quality >= QUALITY_LOW && quality < MAX_TEXTURE_QUALITY_LEVELS)
  136. {
  137. mipsToSkip_[quality] = mips;
  138. // Make sure a higher quality level does not actually skip more mips
  139. for (int i = 1; i < MAX_TEXTURE_QUALITY_LEVELS; ++i)
  140. {
  141. if (mipsToSkip_[i] > mipsToSkip_[i - 1])
  142. mipsToSkip_[i] = mipsToSkip_[i - 1];
  143. }
  144. }
  145. }
  146. bool Texture::IsCompressed() const
  147. {
  148. return format_ == DXGI_FORMAT_BC1_UNORM || format_ == DXGI_FORMAT_BC2_UNORM || format_ == DXGI_FORMAT_BC3_UNORM;
  149. }
  150. int Texture::GetMipsToSkip(int quality) const
  151. {
  152. return (quality >= QUALITY_LOW && quality < MAX_TEXTURE_QUALITY_LEVELS) ? mipsToSkip_[quality] : 0;
  153. }
  154. int Texture::GetLevelWidth(unsigned level) const
  155. {
  156. if (level > levels_)
  157. return 0;
  158. return Max(width_ >> level, 1);
  159. }
  160. int Texture::GetLevelHeight(unsigned level) const
  161. {
  162. if (level > levels_)
  163. return 0;
  164. return Max(height_ >> level, 1);
  165. }
  166. int Texture::GetLevelDepth(unsigned level) const
  167. {
  168. if (level > levels_)
  169. return 0;
  170. return Max(depth_ >> level, 1);
  171. }
  172. unsigned Texture::GetDataSize(int width, int height) const
  173. {
  174. if (IsCompressed())
  175. return GetRowDataSize(width) * ((height + 3) >> 2);
  176. else
  177. return GetRowDataSize(width) * height;
  178. }
  179. unsigned Texture::GetDataSize(int width, int height, int depth) const
  180. {
  181. return depth * GetDataSize(width, height);
  182. }
  183. unsigned Texture::GetRowDataSize(int width) const
  184. {
  185. switch (format_)
  186. {
  187. case DXGI_FORMAT_R8_UNORM:
  188. case DXGI_FORMAT_A8_UNORM:
  189. return width;
  190. case DXGI_FORMAT_R8G8_UNORM:
  191. case DXGI_FORMAT_R16_UNORM:
  192. case DXGI_FORMAT_R16_FLOAT:
  193. case DXGI_FORMAT_R16_TYPELESS:
  194. return width * 2;
  195. case DXGI_FORMAT_R8G8B8A8_UNORM:
  196. case DXGI_FORMAT_R16G16_UNORM:
  197. case DXGI_FORMAT_R16G16_FLOAT:
  198. case DXGI_FORMAT_R32_FLOAT:
  199. case DXGI_FORMAT_R24G8_TYPELESS:
  200. case DXGI_FORMAT_R32_TYPELESS:
  201. return width * 4;
  202. case DXGI_FORMAT_R16G16B16A16_UNORM:
  203. case DXGI_FORMAT_R16G16B16A16_FLOAT:
  204. return width * 8;
  205. case DXGI_FORMAT_R32G32B32A32_FLOAT:
  206. return width * 16;
  207. case DXGI_FORMAT_BC1_UNORM:
  208. return ((width + 3) >> 2) * 8;
  209. case DXGI_FORMAT_BC2_UNORM:
  210. case DXGI_FORMAT_BC3_UNORM:
  211. return ((width + 3) >> 2) * 16;
  212. default:
  213. return 0;
  214. }
  215. }
  216. void Texture::SetParameters(XMLFile* file)
  217. {
  218. if (!file)
  219. return;
  220. XMLElement rootElem = file->GetRoot();
  221. SetParameters(rootElem);
  222. }
  223. void Texture::SetParameters(const XMLElement& element)
  224. {
  225. XMLElement paramElem = element.GetChild();
  226. while (paramElem)
  227. {
  228. String name = paramElem.GetName();
  229. if (name == "address")
  230. {
  231. String coord = paramElem.GetAttributeLower("coord");
  232. if (coord.Length() >= 1)
  233. {
  234. TextureCoordinate coordIndex = (TextureCoordinate)(coord[0] - 'u');
  235. String mode = paramElem.GetAttributeLower("mode");
  236. SetAddressMode(coordIndex, (TextureAddressMode)GetStringListIndex(mode.CString(), addressModeNames, ADDRESS_WRAP));
  237. }
  238. }
  239. if (name == "border")
  240. SetBorderColor(paramElem.GetColor("color"));
  241. if (name == "filter")
  242. {
  243. String mode = paramElem.GetAttributeLower("mode");
  244. SetFilterMode((TextureFilterMode)GetStringListIndex(mode.CString(), filterModeNames, FILTER_DEFAULT));
  245. }
  246. if (name == "mipmap")
  247. SetNumLevels(paramElem.GetBool("enable") ? 0 : 1);
  248. if (name == "quality")
  249. {
  250. if (paramElem.HasAttribute("low"))
  251. SetMipsToSkip(QUALITY_LOW, paramElem.GetInt("low"));
  252. if (paramElem.HasAttribute("med"))
  253. SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("med"));
  254. if (paramElem.HasAttribute("medium"))
  255. SetMipsToSkip(QUALITY_MEDIUM, paramElem.GetInt("medium"));
  256. if (paramElem.HasAttribute("high"))
  257. SetMipsToSkip(QUALITY_HIGH, paramElem.GetInt("high"));
  258. }
  259. if (name == "srgb")
  260. SetSRGB(paramElem.GetBool("enable"));
  261. paramElem = paramElem.GetNext();
  262. }
  263. }
  264. void Texture::SetParametersDirty()
  265. {
  266. parametersDirty_ = true;
  267. }
  268. void Texture::UpdateParameters()
  269. {
  270. if ((!parametersDirty_ && sampler_) || !object_)
  271. return;
  272. // Release old sampler
  273. if (sampler_)
  274. {
  275. ((ID3D11SamplerState*)sampler_)->Release();
  276. sampler_ = 0;
  277. }
  278. D3D11_SAMPLER_DESC samplerDesc;
  279. memset(&samplerDesc, 0, sizeof samplerDesc);
  280. unsigned filterModeIndex = filterMode_ != FILTER_DEFAULT ? filterMode_ : graphics_->GetDefaultTextureFilterMode();
  281. if (shadowCompare_)
  282. filterModeIndex += 4;
  283. samplerDesc.Filter = d3dFilterMode[filterModeIndex];
  284. samplerDesc.AddressU = d3dAddressMode[addressMode_[0]];
  285. samplerDesc.AddressV = d3dAddressMode[addressMode_[1]];
  286. samplerDesc.AddressW = d3dAddressMode[addressMode_[2]];
  287. samplerDesc.MaxAnisotropy = graphics_->GetTextureAnisotropy();
  288. samplerDesc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL;
  289. samplerDesc.MinLOD = -M_INFINITY;
  290. samplerDesc.MaxLOD = M_INFINITY;
  291. memcpy(&samplerDesc.BorderColor, borderColor_.Data(), 4 * sizeof(float));
  292. graphics_->GetImpl()->GetDevice()->CreateSamplerState(&samplerDesc, (ID3D11SamplerState**)&sampler_);
  293. if (!sampler_)
  294. LOGERROR("Failed to create sampler state");
  295. parametersDirty_ = false;
  296. }
  297. unsigned Texture::CheckMaxLevels(int width, int height, unsigned requestedLevels)
  298. {
  299. unsigned maxLevels = 1;
  300. while (width > 1 && height > 1)
  301. {
  302. ++maxLevels;
  303. width >>= 1;
  304. height >>= 1;
  305. }
  306. if (!requestedLevels || maxLevels < requestedLevels)
  307. return maxLevels;
  308. else
  309. return requestedLevels;
  310. }
  311. unsigned Texture::CheckMaxLevels(int width, int height, int depth, unsigned requestedLevels)
  312. {
  313. unsigned maxLevels = 1;
  314. while (width > 1 && height > 1 && depth > 1)
  315. {
  316. ++maxLevels;
  317. width >>= 1;
  318. height >>= 1;
  319. depth >>= 1;
  320. }
  321. if (!requestedLevels || maxLevels < requestedLevels)
  322. return maxLevels;
  323. else
  324. return requestedLevels;
  325. }
  326. unsigned Texture::GetSRVFormat(unsigned format)
  327. {
  328. if (format == DXGI_FORMAT_R24G8_TYPELESS)
  329. return DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
  330. else if (format == DXGI_FORMAT_R16_TYPELESS)
  331. return DXGI_FORMAT_R16_UNORM;
  332. else if (format == DXGI_FORMAT_R32_TYPELESS)
  333. return DXGI_FORMAT_R32_FLOAT;
  334. else
  335. return format;
  336. }
  337. unsigned Texture::GetDSVFormat(unsigned format)
  338. {
  339. if (format == DXGI_FORMAT_R24G8_TYPELESS)
  340. return DXGI_FORMAT_D24_UNORM_S8_UINT;
  341. else if (format == DXGI_FORMAT_R16_TYPELESS)
  342. return DXGI_FORMAT_D16_UNORM;
  343. else if (format == DXGI_FORMAT_R32_TYPELESS)
  344. return DXGI_FORMAT_D32_FLOAT;
  345. else
  346. return format;
  347. }
  348. unsigned Texture::GetSRGBFormat(unsigned format)
  349. {
  350. if (format == DXGI_FORMAT_R8G8B8A8_UNORM)
  351. return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
  352. else if (format == DXGI_FORMAT_BC1_UNORM)
  353. return DXGI_FORMAT_BC1_UNORM_SRGB;
  354. else if (format == DXGI_FORMAT_BC2_UNORM)
  355. return DXGI_FORMAT_BC2_UNORM_SRGB;
  356. else if (format == DXGI_FORMAT_BC3_UNORM)
  357. return DXGI_FORMAT_BC3_UNORM_SRGB;
  358. else
  359. return format;
  360. }
  361. void Texture::CheckTextureBudget(StringHash type)
  362. {
  363. ResourceCache* cache = GetSubsystem<ResourceCache>();
  364. unsigned textureBudget = cache->GetMemoryBudget(type);
  365. unsigned textureUse = cache->GetMemoryUse(type);
  366. if (!textureBudget)
  367. return;
  368. // If textures are over the budget, they likely can not be freed directly as materials still refer to them.
  369. // Therefore free unused materials first
  370. if (textureUse > textureBudget)
  371. cache->ReleaseResources(Material::GetTypeStatic());
  372. }
  373. }