BsD3D11SamplerState.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "BsD3D11SamplerState.h"
  2. #include "BsD3D11RenderAPI.h"
  3. #include "BsD3D11Device.h"
  4. #include "BsD3D11Mappings.h"
  5. #include "BsRenderStats.h"
  6. namespace BansheeEngine
  7. {
  8. D3D11SamplerStateCore::D3D11SamplerStateCore(const SAMPLER_STATE_DESC& desc)
  9. :SamplerStateCore(desc), mSamplerState(nullptr)
  10. { }
  11. D3D11SamplerStateCore::~D3D11SamplerStateCore()
  12. {
  13. SAFE_RELEASE(mSamplerState);
  14. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_SamplerState);
  15. }
  16. void D3D11SamplerStateCore::createInternal()
  17. {
  18. D3D11_SAMPLER_DESC samplerState;
  19. ZeroMemory(&samplerState, sizeof(D3D11_SAMPLER_DESC));
  20. samplerState.AddressU = D3D11Mappings::get(mProperties.getTextureAddressingMode().u);
  21. samplerState.AddressV = D3D11Mappings::get(mProperties.getTextureAddressingMode().v);
  22. samplerState.AddressW = D3D11Mappings::get(mProperties.getTextureAddressingMode().w);
  23. samplerState.BorderColor[0] = mProperties.getBorderColor()[0];
  24. samplerState.BorderColor[1] = mProperties.getBorderColor()[1];
  25. samplerState.BorderColor[2] = mProperties.getBorderColor()[2];
  26. samplerState.BorderColor[3] = mProperties.getBorderColor()[3];
  27. samplerState.ComparisonFunc = D3D11Mappings::get(mProperties.getComparisonFunction());
  28. samplerState.MaxAnisotropy = mProperties.getTextureAnisotropy();
  29. samplerState.MaxLOD = mProperties.getMaximumMip();
  30. samplerState.MinLOD = mProperties.getMinimumMip();
  31. samplerState.MipLODBias = mProperties.getTextureMipmapBias();
  32. bool isComparison = ((mProperties.getTextureFiltering(FT_MIN) & FO_USE_COMPARISON) &
  33. (mProperties.getTextureFiltering(FT_MAG) & FO_USE_COMPARISON) &
  34. (mProperties.getTextureFiltering(FT_MIP) & FO_USE_COMPARISON)) != 0;
  35. FilterOptions minFilter = (FilterOptions)(mProperties.getTextureFiltering(FT_MIN) & ~FO_USE_COMPARISON);
  36. FilterOptions magFilter = (FilterOptions)(mProperties.getTextureFiltering(FT_MAG) & ~FO_USE_COMPARISON);
  37. FilterOptions mipFilter = (FilterOptions)(mProperties.getTextureFiltering(FT_MIP) & ~FO_USE_COMPARISON);
  38. if (minFilter == FO_ANISOTROPIC && magFilter == FO_ANISOTROPIC && mipFilter == FO_ANISOTROPIC)
  39. {
  40. samplerState.Filter = D3D11_FILTER_ANISOTROPIC;
  41. }
  42. else
  43. {
  44. if(minFilter == FO_POINT || minFilter == FO_NONE)
  45. {
  46. if(magFilter == FO_POINT || magFilter == FO_NONE)
  47. {
  48. if(mipFilter == FO_POINT || mipFilter == FO_NONE)
  49. samplerState.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
  50. else if(mipFilter == FO_LINEAR)
  51. samplerState.Filter = D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
  52. }
  53. else if(magFilter == FO_LINEAR)
  54. {
  55. if(mipFilter == FO_POINT || mipFilter == FO_NONE)
  56. samplerState.Filter = D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
  57. else if(mipFilter == FO_LINEAR)
  58. samplerState.Filter = D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR;
  59. }
  60. }
  61. else if(minFilter == FO_LINEAR)
  62. {
  63. if(magFilter == FO_POINT || magFilter == FO_NONE)
  64. {
  65. if(mipFilter == FO_POINT || mipFilter == FO_NONE)
  66. samplerState.Filter = D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT;
  67. else if(mipFilter == FO_LINEAR)
  68. samplerState.Filter = D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
  69. }
  70. else if(magFilter == FO_LINEAR)
  71. {
  72. if(mipFilter == FO_POINT || mipFilter == FO_NONE)
  73. samplerState.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
  74. else if(mipFilter == FO_LINEAR)
  75. samplerState.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  76. }
  77. }
  78. }
  79. if(isComparison)
  80. {
  81. // Adds COMPARISON flag to the filter
  82. // See: http://msdn.microsoft.com/en-us/library/windows/desktop/ff476132(v=vs.85).aspx
  83. samplerState.Filter = (D3D11_FILTER)(0x80 | samplerState.Filter);
  84. }
  85. D3D11RenderAPI* rs = static_cast<D3D11RenderAPI*>(RenderAPICore::instancePtr());
  86. D3D11Device& device = rs->getPrimaryDevice();
  87. HRESULT hr = device.getD3D11Device()->CreateSamplerState(&samplerState, &mSamplerState);
  88. if(FAILED(hr) || device.hasError())
  89. {
  90. String errorDescription = device.getErrorDescription();
  91. BS_EXCEPT(RenderingAPIException, "Cannot create sampler state.\nError Description:" + errorDescription);
  92. }
  93. BS_INC_RENDER_STAT_CAT(ResCreated, RenderStatObject_SamplerState);
  94. SamplerStateCore::createInternal();
  95. }
  96. }