1
0

d3d11-samplerstate.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <graphics/vec4.h>
  15. #include <float.h>
  16. #include "d3d11-subsystem.hpp"
  17. static inline D3D11_TEXTURE_ADDRESS_MODE ConvertGSAddressMode(gs_address_mode mode)
  18. {
  19. switch (mode) {
  20. case GS_ADDRESS_WRAP:
  21. return D3D11_TEXTURE_ADDRESS_WRAP;
  22. case GS_ADDRESS_CLAMP:
  23. return D3D11_TEXTURE_ADDRESS_CLAMP;
  24. case GS_ADDRESS_MIRROR:
  25. return D3D11_TEXTURE_ADDRESS_MIRROR;
  26. case GS_ADDRESS_BORDER:
  27. return D3D11_TEXTURE_ADDRESS_BORDER;
  28. case GS_ADDRESS_MIRRORONCE:
  29. return D3D11_TEXTURE_ADDRESS_MIRROR_ONCE;
  30. }
  31. return D3D11_TEXTURE_ADDRESS_WRAP;
  32. }
  33. static inline D3D11_FILTER ConvertGSFilter(gs_sample_filter filter)
  34. {
  35. switch (filter) {
  36. case GS_FILTER_POINT:
  37. return D3D11_FILTER_MIN_MAG_MIP_POINT;
  38. case GS_FILTER_LINEAR:
  39. return D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  40. case GS_FILTER_MIN_MAG_POINT_MIP_LINEAR:
  41. return D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
  42. case GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT:
  43. return D3D11_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
  44. case GS_FILTER_MIN_POINT_MAG_MIP_LINEAR:
  45. return D3D11_FILTER_MIN_POINT_MAG_MIP_LINEAR;
  46. case GS_FILTER_MIN_LINEAR_MAG_MIP_POINT:
  47. return D3D11_FILTER_MIN_LINEAR_MAG_MIP_POINT;
  48. case GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR:
  49. return D3D11_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
  50. case GS_FILTER_MIN_MAG_LINEAR_MIP_POINT:
  51. return D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
  52. case GS_FILTER_ANISOTROPIC:
  53. return D3D11_FILTER_ANISOTROPIC;
  54. }
  55. return D3D11_FILTER_MIN_MAG_MIP_POINT;
  56. }
  57. gs_sampler_state::gs_sampler_state(gs_device_t *device, const gs_sampler_info *info)
  58. : gs_obj(device, gs_type::gs_sampler_state),
  59. info(*info)
  60. {
  61. HRESULT hr;
  62. vec4 v4;
  63. memset(&sd, 0, sizeof(sd));
  64. sd.AddressU = ConvertGSAddressMode(info->address_u);
  65. sd.AddressV = ConvertGSAddressMode(info->address_v);
  66. sd.AddressW = ConvertGSAddressMode(info->address_w);
  67. sd.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  68. sd.Filter = ConvertGSFilter(info->filter);
  69. sd.MaxAnisotropy = info->max_anisotropy;
  70. sd.MaxLOD = FLT_MAX;
  71. vec4_from_rgba(&v4, info->border_color);
  72. memcpy(sd.BorderColor, v4.ptr, sizeof(v4));
  73. hr = device->device->CreateSamplerState(&sd, state.Assign());
  74. if (FAILED(hr))
  75. throw HRError("Failed to create sampler state", hr);
  76. }