BsD3D11SamplerState.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsD3D11SamplerState.h"
  4. #include "BsD3D11RenderAPI.h"
  5. #include "BsD3D11Device.h"
  6. #include "BsD3D11Mappings.h"
  7. #include "Profiling/BsRenderStats.h"
  8. namespace bs { namespace ct
  9. {
  10. D3D11SamplerState::D3D11SamplerState(const SAMPLER_STATE_DESC& desc, GpuDeviceFlags deviceMask)
  11. :SamplerState(desc, deviceMask), mSamplerState(nullptr)
  12. { }
  13. D3D11SamplerState::~D3D11SamplerState()
  14. {
  15. SAFE_RELEASE(mSamplerState);
  16. BS_INC_RENDER_STAT_CAT(ResDestroyed, RenderStatObject_SamplerState);
  17. }
  18. void D3D11SamplerState::createInternal()
  19. {
  20. D3D11_SAMPLER_DESC samplerState;
  21. ZeroMemory(&samplerState, sizeof(D3D11_SAMPLER_DESC));
  22. samplerState.AddressU = D3D11Mappings::get(mProperties.getTextureAddressingMode().u);
  23. samplerState.AddressV = D3D11Mappings::get(mProperties.getTextureAddressingMode().v);
  24. samplerState.AddressW = D3D11Mappings::get(mProperties.getTextureAddressingMode().w);
  25. samplerState.BorderColor[0] = mProperties.getBorderColor()[0];
  26. samplerState.BorderColor[1] = mProperties.getBorderColor()[1];
  27. samplerState.BorderColor[2] = mProperties.getBorderColor()[2];
  28. samplerState.BorderColor[3] = mProperties.getBorderColor()[3];
  29. samplerState.ComparisonFunc = D3D11Mappings::get(mProperties.getComparisonFunction());
  30. samplerState.MaxAnisotropy = mProperties.getTextureAnisotropy();
  31. samplerState.MaxLOD = mProperties.getMaximumMip();
  32. samplerState.MinLOD = mProperties.getMinimumMip();
  33. samplerState.MipLODBias = mProperties.getTextureMipmapBias();
  34. FilterOptions minFilter = mProperties.getTextureFiltering(FT_MIN);
  35. FilterOptions magFilter = mProperties.getTextureFiltering(FT_MAG);
  36. FilterOptions mipFilter = mProperties.getTextureFiltering(FT_MIP);
  37. if (minFilter == FO_ANISOTROPIC && magFilter == FO_ANISOTROPIC && mipFilter == FO_ANISOTROPIC)
  38. {
  39. samplerState.Filter = D3D11_FILTER_ANISOTROPIC;
  40. }
  41. else
  42. {
  43. if(minFilter == FO_POINT || minFilter == FO_NONE)
  44. {
  45. if(magFilter == FO_POINT || magFilter == FO_NONE)
  46. {
  47. if(mipFilter == FO_POINT || mipFilter == FO_NONE)
  48. samplerState.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
  49. else if(mipFilter == FO_LINEAR)
  50. samplerState.Filter = D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
  51. }
  52. else if(magFilter == FO_LINEAR)
  53. {
  54. if(mipFilter == FO_POINT || mipFilter == FO_NONE)
  55. samplerState.Filter = D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
  56. else if(mipFilter == FO_LINEAR)
  57. samplerState.Filter = D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR;
  58. }
  59. }
  60. else if(minFilter == FO_LINEAR)
  61. {
  62. if(magFilter == FO_POINT || magFilter == FO_NONE)
  63. {
  64. if(mipFilter == FO_POINT || mipFilter == FO_NONE)
  65. samplerState.Filter = D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT;
  66. else if(mipFilter == FO_LINEAR)
  67. samplerState.Filter = D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
  68. }
  69. else if(magFilter == FO_LINEAR)
  70. {
  71. if(mipFilter == FO_POINT || mipFilter == FO_NONE)
  72. samplerState.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
  73. else if(mipFilter == FO_LINEAR)
  74. samplerState.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  75. }
  76. }
  77. }
  78. bool isComparison = mProperties.getComparisonFunction() != CMPF_ALWAYS_PASS;
  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*>(RenderAPI::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. SamplerState::createInternal();
  95. }
  96. }}