D3D11Texture.cpp 12 KB

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