D3D11Texture.cpp 6.2 KB

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