2
0

sampler.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //! Samplers as the host describes them, from the console's sampler
  2. //! description (`CreateSamplerX`), which names the hardware's sampler
  3. //! fields. (Immediate samplers in shader code come decoded with the
  4. //! translation.)
  5. use windows::Win32::Graphics::Direct3D12::*;
  6. /// The fields both forms share.
  7. #[derive(Debug, Clone, Copy, PartialEq)]
  8. struct Fields {
  9. /// Point 0, bilinear 1, anisotropic point 2, anisotropic bilinear 3.
  10. mag: u32,
  11. min: u32,
  12. /// None 0, point 1, linear 2.
  13. mip: u32,
  14. /// Plain 0, minimum 1, maximum 2.
  15. mode: u32,
  16. /// Host address modes.
  17. address: [u32; 3],
  18. anisotropy: u32,
  19. /// Host comparison function.
  20. compare: u32,
  21. min_lod: f32,
  22. max_lod: f32,
  23. bias: f32,
  24. border: [f32; 4],
  25. }
  26. impl Fields {
  27. fn host(&self) -> D3D12_SAMPLER_DESC {
  28. let linear = |f: u32| u32::from(f == 1 || f == 3);
  29. let aniso = self.mag >= 2 || self.min >= 2;
  30. // The console compares when the instruction says so, with the
  31. // sampler's function; the host needs a comparison sampler for that.
  32. // A sampler given a real test (less ... greater-or-equal) is one;
  33. // never, always and none are plain samplers.
  34. const LESS: u32 = 2;
  35. const GREATER_EQUAL: u32 = 7;
  36. let compares = (LESS..=GREATER_EQUAL).contains(&self.compare);
  37. let reduction = match self.mode {
  38. 1 => 2,
  39. 2 => 3,
  40. _ => u32::from(compares),
  41. };
  42. let filter = if aniso {
  43. 0x55
  44. } else {
  45. linear(self.min) << 4 | linear(self.mag) << 2 | u32::from(self.mip == 2)
  46. };
  47. D3D12_SAMPLER_DESC {
  48. Filter: D3D12_FILTER((reduction << 7 | filter) as i32),
  49. AddressU: D3D12_TEXTURE_ADDRESS_MODE(self.address[0] as i32),
  50. AddressV: D3D12_TEXTURE_ADDRESS_MODE(self.address[1] as i32),
  51. AddressW: D3D12_TEXTURE_ADDRESS_MODE(self.address[2] as i32),
  52. MipLODBias: self.bias,
  53. MaxAnisotropy: if aniso {
  54. self.anisotropy.clamp(1, 16)
  55. } else {
  56. 1
  57. },
  58. ComparisonFunc: D3D12_COMPARISON_FUNC(self.compare.max(1) as i32),
  59. BorderColor: self.border,
  60. MinLOD: self.min_lod,
  61. // Without a mip filter the console samples mip 0 only.
  62. MaxLOD: if self.mip == 0 {
  63. self.min_lod
  64. } else {
  65. self.max_lod
  66. },
  67. }
  68. }
  69. }
  70. /// The console's sampler description (27 words): flags, magnification,
  71. /// minification, mip and depth filters, filter mode, three address modes,
  72. /// minimum and maximum LOD, LOD bias, secondary bias, anisotropy, its bias
  73. /// and threshold, comparison, border color, then tuning words.
  74. pub(crate) fn console(words: &[u32; 27]) -> D3D12_SAMPLER_DESC {
  75. let f = |i: usize| f32::from_bits(words[i]);
  76. Fields {
  77. mag: words[1],
  78. min: words[2],
  79. mip: words[3],
  80. mode: words[5],
  81. address: [words[6], words[7], words[8]],
  82. min_lod: f(9),
  83. max_lod: f(10),
  84. bias: f(11),
  85. anisotropy: words[13],
  86. compare: words[16],
  87. border: [f(17), f(18), f(19), f(20)],
  88. }
  89. .host()
  90. }
  91. #[cfg(test)]
  92. mod tests {
  93. use super::*;
  94. #[test]
  95. fn console_descriptions_decode() {
  96. let mut w = [0u32; 27];
  97. w[1] = 1; // bilinear magnification
  98. w[2] = 0; // point minification
  99. w[3] = 2; // linear mips
  100. w[6..9].copy_from_slice(&[3, 3, 3]);
  101. w[10] = 1000.0f32.to_bits();
  102. let d = console(&w);
  103. assert_eq!(d.Filter.0, 0x5, "min point, mag and mip linear");
  104. assert_eq!(d.AddressW, D3D12_TEXTURE_ADDRESS_MODE_CLAMP);
  105. assert_eq!(d.MaxLOD, 1000.0);
  106. // A less-or-equal test makes a comparison sampler; never does not.
  107. w[16] = 4;
  108. assert_eq!(console(&w).Filter.0, 0x85);
  109. assert_eq!(console(&w).ComparisonFunc, D3D12_COMPARISON_FUNC_LESS_EQUAL);
  110. w[16] = 1;
  111. assert_eq!(console(&w).Filter.0, 0x5);
  112. }
  113. }