sampler.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. let min_lod = finite_lod(self.min_lod, 0.0);
  48. // Without a mip filter the console clamps sampling to `min_lod`.
  49. let max_lod = if self.mip == 0 {
  50. min_lod
  51. } else {
  52. finite_lod(self.max_lod, min_lod).max(min_lod)
  53. };
  54. D3D12_SAMPLER_DESC {
  55. Filter: D3D12_FILTER((reduction << 7 | filter) as i32),
  56. AddressU: address_mode(self.address[0]),
  57. AddressV: address_mode(self.address[1]),
  58. AddressW: address_mode(self.address[2]),
  59. MipLODBias: if self.bias.is_nan() {
  60. 0.0
  61. } else {
  62. self.bias
  63. .clamp(D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX)
  64. },
  65. MaxAnisotropy: if aniso {
  66. self.anisotropy.clamp(1, 16)
  67. } else {
  68. 1
  69. },
  70. ComparisonFunc: if reduction == 1 {
  71. D3D12_COMPARISON_FUNC(self.compare as i32)
  72. } else {
  73. D3D12_COMPARISON_FUNC_NEVER
  74. },
  75. BorderColor: self.border,
  76. MinLOD: min_lod,
  77. MaxLOD: max_lod,
  78. }
  79. }
  80. }
  81. /// Maps a console address mode (`1..=5`) to host D3D12, falling back to `CLAMP`.
  82. fn address_mode(mode: u32) -> D3D12_TEXTURE_ADDRESS_MODE {
  83. match mode {
  84. 1..=5 => D3D12_TEXTURE_ADDRESS_MODE(mode as i32),
  85. _ => D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
  86. }
  87. }
  88. /// Sanitizes an LOD bound for D3D12, substituting `fallback` for `NaN` and clamping infinities.
  89. fn finite_lod(value: f32, fallback: f32) -> f32 {
  90. if value.is_nan() {
  91. fallback
  92. } else {
  93. value.clamp(-D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX)
  94. }
  95. }
  96. /// The console's sampler description (27 words): flags, magnification,
  97. /// minification, mip and depth filters, filter mode, three address modes,
  98. /// minimum and maximum LOD, LOD bias, secondary bias, anisotropy, its bias
  99. /// and threshold, comparison, border color, then tuning words.
  100. pub(crate) fn console(words: &[u32; 27]) -> D3D12_SAMPLER_DESC {
  101. let f = |i: usize| f32::from_bits(words[i]);
  102. Fields {
  103. mag: words[1],
  104. min: words[2],
  105. mip: words[3],
  106. mode: words[5],
  107. address: [words[6], words[7], words[8]],
  108. min_lod: f(9),
  109. max_lod: f(10),
  110. bias: f(11),
  111. anisotropy: words[13],
  112. compare: words[16],
  113. border: [f(17), f(18), f(19), f(20)],
  114. }
  115. .host()
  116. }
  117. #[cfg(test)]
  118. mod tests {
  119. use super::*;
  120. #[test]
  121. fn console_descriptions_decode() {
  122. let mut w = [0u32; 27];
  123. w[1] = 1; // bilinear magnification
  124. w[2] = 0; // point minification
  125. w[3] = 2; // linear mips
  126. w[6..9].copy_from_slice(&[3, 3, 3]);
  127. w[10] = 1000.0f32.to_bits();
  128. let d = console(&w);
  129. assert_eq!(d.Filter.0, 0x5, "min point, mag and mip linear");
  130. assert_eq!(d.AddressW, D3D12_TEXTURE_ADDRESS_MODE_CLAMP);
  131. assert_eq!(d.MaxLOD, 1000.0);
  132. // A less-or-equal test makes a comparison sampler; never does not.
  133. w[16] = 4;
  134. assert_eq!(console(&w).Filter.0, 0x85);
  135. assert_eq!(console(&w).ComparisonFunc, D3D12_COMPARISON_FUNC_LESS_EQUAL);
  136. w[16] = 1;
  137. assert_eq!(console(&w).Filter.0, 0x5);
  138. }
  139. #[test]
  140. fn mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps() {
  141. let mut w = [0u32; 27];
  142. w[3] = 0; // mip none
  143. w[9] = 3.0f32.to_bits();
  144. w[10] = 8.0f32.to_bits();
  145. let d = console(&w);
  146. assert_eq!((d.MinLOD, d.MaxLOD), (3.0, 3.0));
  147. w[9] = f32::NAN.to_bits();
  148. w[10] = 8.0f32.to_bits();
  149. let d = console(&w);
  150. assert_eq!((d.MinLOD, d.MaxLOD), (0.0, 0.0));
  151. w[9] = f32::INFINITY.to_bits();
  152. w[10] = 8.0f32.to_bits();
  153. let d = console(&w);
  154. assert_eq!((d.MinLOD, d.MaxLOD), (D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX));
  155. w[3] = 2; // linear mips
  156. w[9] = 4.0f32.to_bits();
  157. w[10] = 1.0f32.to_bits();
  158. let d = console(&w);
  159. assert_eq!((d.MinLOD, d.MaxLOD), (4.0, 4.0));
  160. w[9] = f32::NAN.to_bits();
  161. w[10] = f32::INFINITY.to_bits();
  162. let d = console(&w);
  163. assert_eq!((d.MinLOD, d.MaxLOD), (0.0, D3D12_FLOAT32_MAX));
  164. w[9] = 2.5f32.to_bits();
  165. w[10] = f32::NAN.to_bits();
  166. let d = console(&w);
  167. assert_eq!((d.MinLOD, d.MaxLOD), (2.5, 2.5));
  168. w[9] = f32::NEG_INFINITY.to_bits();
  169. w[10] = f32::NEG_INFINITY.to_bits();
  170. let d = console(&w);
  171. assert_eq!(
  172. (d.MinLOD, d.MaxLOD),
  173. (-D3D12_FLOAT32_MAX, -D3D12_FLOAT32_MAX)
  174. );
  175. }
  176. #[test]
  177. fn bias_address_and_comparison_stay_in_host_bounds() {
  178. let mut w = [0u32; 27];
  179. w[6..9].copy_from_slice(&[0, 6, 3]);
  180. w[11] = 24.0f32.to_bits();
  181. w[16] = 99;
  182. let d = console(&w);
  183. assert_eq!(d.MipLODBias, D3D12_MIP_LOD_BIAS_MAX);
  184. assert_eq!(
  185. [d.AddressU, d.AddressV, d.AddressW],
  186. [D3D12_TEXTURE_ADDRESS_MODE_CLAMP; 3]
  187. );
  188. assert_eq!(d.ComparisonFunc, D3D12_COMPARISON_FUNC_NEVER);
  189. w[11] = (-24.0f32).to_bits();
  190. w[5] = 1; // minimum reduction
  191. w[16] = 4; // less-or-equal, ignored when reduction != 1
  192. let d = console(&w);
  193. assert_eq!(d.MipLODBias, D3D12_MIP_LOD_BIAS_MIN);
  194. assert_eq!(d.ComparisonFunc, D3D12_COMPARISON_FUNC_NEVER);
  195. w[11] = f32::NAN.to_bits();
  196. w[5] = 0; // plain mode, reduction == 1 from compare == 4
  197. w[6..9].copy_from_slice(&[1, 4, 5]);
  198. w[16] = 4;
  199. let d = console(&w);
  200. assert_eq!(d.MipLODBias, 0.0);
  201. assert_eq!(
  202. [d.AddressU, d.AddressV, d.AddressW],
  203. [
  204. D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  205. D3D12_TEXTURE_ADDRESS_MODE_BORDER,
  206. D3D12_TEXTURE_ADDRESS_MODE_MIRROR_ONCE,
  207. ]
  208. );
  209. assert_eq!(d.ComparisonFunc, D3D12_COMPARISON_FUNC_LESS_EQUAL);
  210. }
  211. }