D3D11Texture.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //
  2. // Copyright (c) 2008-2017 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 D3D11_FILTER d3dFilterMode[] =
  35. {
  36. D3D11_FILTER_MIN_MAG_MIP_POINT,
  37. D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT,
  38. D3D11_FILTER_MIN_MAG_MIP_LINEAR,
  39. D3D11_FILTER_ANISOTROPIC,
  40. D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR,
  41. D3D11_FILTER_COMPARISON_MIN_MAG_MIP_POINT,
  42. D3D11_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT,
  43. D3D11_FILTER_COMPARISON_MIN_MAG_MIP_LINEAR,
  44. D3D11_FILTER_COMPARISON_ANISOTROPIC,
  45. D3D11_FILTER_COMPARISON_MIN_MAG_POINT_MIP_LINEAR
  46. };
  47. static const D3D11_TEXTURE_ADDRESS_MODE d3dAddressMode[] =
  48. {
  49. D3D11_TEXTURE_ADDRESS_WRAP,
  50. D3D11_TEXTURE_ADDRESS_MIRROR,
  51. D3D11_TEXTURE_ADDRESS_CLAMP,
  52. D3D11_TEXTURE_ADDRESS_BORDER
  53. };
  54. void Texture::SetSRGB(bool enable)
  55. {
  56. if (graphics_)
  57. enable &= graphics_->GetSRGBSupport();
  58. if (enable != sRGB_)
  59. {
  60. sRGB_ = enable;
  61. // If texture had already been created, must recreate it to set the sRGB texture format
  62. if (object_.name_)
  63. Create();
  64. }
  65. }
  66. bool Texture::GetParametersDirty() const
  67. {
  68. return parametersDirty_ || !sampler_;
  69. }
  70. bool Texture::IsCompressed() const
  71. {
  72. return format_ == DXGI_FORMAT_BC1_UNORM || format_ == DXGI_FORMAT_BC2_UNORM || format_ == DXGI_FORMAT_BC3_UNORM;
  73. }
  74. unsigned Texture::GetRowDataSize(int width) const
  75. {
  76. switch (format_)
  77. {
  78. case DXGI_FORMAT_R8_UNORM:
  79. case DXGI_FORMAT_A8_UNORM:
  80. return (unsigned)width;
  81. case DXGI_FORMAT_R8G8_UNORM:
  82. case DXGI_FORMAT_R16_UNORM:
  83. case DXGI_FORMAT_R16_FLOAT:
  84. case DXGI_FORMAT_R16_TYPELESS:
  85. return (unsigned)(width * 2);
  86. case DXGI_FORMAT_R8G8B8A8_UNORM:
  87. case DXGI_FORMAT_R16G16_UNORM:
  88. case DXGI_FORMAT_R16G16_FLOAT:
  89. case DXGI_FORMAT_R32_FLOAT:
  90. case DXGI_FORMAT_R24G8_TYPELESS:
  91. case DXGI_FORMAT_R32_TYPELESS:
  92. return (unsigned)(width * 4);
  93. case DXGI_FORMAT_R16G16B16A16_UNORM:
  94. case DXGI_FORMAT_R16G16B16A16_FLOAT:
  95. return (unsigned)(width * 8);
  96. case DXGI_FORMAT_R32G32B32A32_FLOAT:
  97. return (unsigned)(width * 16);
  98. case DXGI_FORMAT_BC1_UNORM:
  99. return (unsigned)(((width + 3) >> 2) * 8);
  100. case DXGI_FORMAT_BC2_UNORM:
  101. case DXGI_FORMAT_BC3_UNORM:
  102. return (unsigned)(((width + 3) >> 2) * 16);
  103. default:
  104. return 0;
  105. }
  106. }
  107. void Texture::UpdateParameters()
  108. {
  109. if ((!parametersDirty_ && sampler_) || !object_.ptr_)
  110. return;
  111. // Release old sampler
  112. ATOMIC_SAFE_RELEASE(sampler_);
  113. D3D11_SAMPLER_DESC samplerDesc;
  114. memset(&samplerDesc, 0, sizeof samplerDesc);
  115. unsigned filterModeIndex = filterMode_ != FILTER_DEFAULT ? filterMode_ : graphics_->GetDefaultTextureFilterMode();
  116. if (shadowCompare_)
  117. filterModeIndex += 5;
  118. samplerDesc.Filter = d3dFilterMode[filterModeIndex];
  119. samplerDesc.AddressU = d3dAddressMode[addressMode_[0]];
  120. samplerDesc.AddressV = d3dAddressMode[addressMode_[1]];
  121. samplerDesc.AddressW = d3dAddressMode[addressMode_[2]];
  122. samplerDesc.MaxAnisotropy = anisotropy_ ? anisotropy_ : graphics_->GetDefaultTextureAnisotropy();
  123. samplerDesc.ComparisonFunc = D3D11_COMPARISON_LESS_EQUAL;
  124. samplerDesc.MinLOD = -M_INFINITY;
  125. samplerDesc.MaxLOD = M_INFINITY;
  126. memcpy(&samplerDesc.BorderColor, borderColor_.Data(), 4 * sizeof(float));
  127. HRESULT hr = graphics_->GetImpl()->GetDevice()->CreateSamplerState(&samplerDesc, (ID3D11SamplerState**)&sampler_);
  128. if (FAILED(hr))
  129. {
  130. ATOMIC_SAFE_RELEASE(sampler_);
  131. ATOMIC_LOGD3DERROR("Failed to create sampler state", hr);
  132. }
  133. parametersDirty_ = false;
  134. }
  135. unsigned Texture::GetSRVFormat(unsigned format)
  136. {
  137. if (format == DXGI_FORMAT_R24G8_TYPELESS)
  138. return DXGI_FORMAT_R24_UNORM_X8_TYPELESS;
  139. else if (format == DXGI_FORMAT_R16_TYPELESS)
  140. return DXGI_FORMAT_R16_UNORM;
  141. else if (format == DXGI_FORMAT_R32_TYPELESS)
  142. return DXGI_FORMAT_R32_FLOAT;
  143. else
  144. return format;
  145. }
  146. unsigned Texture::GetDSVFormat(unsigned format)
  147. {
  148. if (format == DXGI_FORMAT_R24G8_TYPELESS)
  149. return DXGI_FORMAT_D24_UNORM_S8_UINT;
  150. else if (format == DXGI_FORMAT_R16_TYPELESS)
  151. return DXGI_FORMAT_D16_UNORM;
  152. else if (format == DXGI_FORMAT_R32_TYPELESS)
  153. return DXGI_FORMAT_D32_FLOAT;
  154. else
  155. return format;
  156. }
  157. unsigned Texture::GetSRGBFormat(unsigned format)
  158. {
  159. if (format == DXGI_FORMAT_R8G8B8A8_UNORM)
  160. return DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;
  161. else if (format == DXGI_FORMAT_BC1_UNORM)
  162. return DXGI_FORMAT_BC1_UNORM_SRGB;
  163. else if (format == DXGI_FORMAT_BC2_UNORM)
  164. return DXGI_FORMAT_BC2_UNORM_SRGB;
  165. else if (format == DXGI_FORMAT_BC3_UNORM)
  166. return DXGI_FORMAT_BC3_UNORM_SRGB;
  167. else
  168. return format;
  169. }
  170. void Texture::RegenerateLevels()
  171. {
  172. if (!shaderResourceView_)
  173. return;
  174. graphics_->GetImpl()->GetDeviceContext()->GenerateMips((ID3D11ShaderResourceView*)shaderResourceView_);
  175. levelsDirty_ = false;
  176. }
  177. // ATOMIC BEGIN
  178. // Satisfy script binding link
  179. unsigned Texture::GetExternalFormat(unsigned format)
  180. {
  181. return 0;
  182. }
  183. unsigned Texture::GetDataType(unsigned format)
  184. {
  185. return 0;
  186. }
  187. // ATOMIC END
  188. }