waterfall_tests.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. //! End-to-end tests of waterfall loops over per-lane descriptors: the
  2. //! synthetic GFX10 programs of `resource::tests` lowered to DXIL, validated
  3. //! by `dxil.dll` when present, and run on the host GPU and WARP, each lane
  4. //! reading the resource its own descriptor names.
  5. use windows::Win32::Graphics::Direct3D12::*;
  6. use super::host::{Dispatch, Host, Root, Stride, View};
  7. use super::{HostShader, lower, validator};
  8. use crate::native::{
  9. ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource,
  10. };
  11. use crate::{io, ir, resource, structure};
  12. /// Lowers a wave32 compute program of 32 threads with the table of views at
  13. /// s0 (textures, or with `buffers` structured buffers of dwords, at entries
  14. /// 0 and 1, a raw UAV at entry 2) and the table of samplers at s1.
  15. fn build(words: &[u32], buffers: bool) -> HostShader {
  16. let table = |user_data: u8| RootParam {
  17. root: RootType::Table,
  18. table_offset_dwords: 0,
  19. sgprs: 1,
  20. base_register: 0,
  21. user_data,
  22. slot: user_data,
  23. };
  24. let range = |kind, table_offset_dwords, dwords, stride, user_data| UsedResource {
  25. kind,
  26. root: RootType::Table,
  27. table_offset_dwords,
  28. lower_bound: 0,
  29. stride,
  30. dwords,
  31. user_data,
  32. slot: user_data,
  33. };
  34. let views = if buffers {
  35. range(ResourceType::Srv, 0, 4, 4, 0)
  36. } else {
  37. range(ResourceType::Srv, 0, 8, 0, 0)
  38. };
  39. let native = NativeProgram {
  40. stage: HwStage::Cs,
  41. wave64: false,
  42. vgprs: 32,
  43. layout: vec![table(0), table(1)],
  44. used: vec![
  45. views,
  46. range(ResourceType::Uav, 16, 4, 0, 0),
  47. range(ResourceType::Sampler, 0, 4, 0, 1),
  48. ],
  49. io: StageIo::Cs(ComputeIo {
  50. rsrc2: 2 << 1,
  51. threads: [32, 1, 1],
  52. }),
  53. code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
  54. };
  55. let p = ir::build(&native.code, native.wave64, native.vgprs).expect("ir::build");
  56. let st = structure::structure(&p).expect("structure");
  57. let res = resource::track(&p, &native.layout, native.stage, Some(&st)).expect("track");
  58. let io = io::raise(&p, &native).expect("io::raise");
  59. let lowered = lower::lower(&lower::Parts {
  60. native: &native,
  61. p: &p,
  62. st: &st,
  63. io: &io,
  64. res: &res,
  65. inputs: Vec::new(),
  66. outputs: Vec::new(),
  67. prims: Vec::new(),
  68. mesh_stride: 0,
  69. })
  70. .expect("lower");
  71. // One access with each lane's own heap index, no wave operation.
  72. assert_eq!(lowered.nonuniform_handles, 1);
  73. assert!(!lowered.wave_ops);
  74. let shader = HostShader {
  75. stage: HwStage::Cs,
  76. dxil: lowered.dxil,
  77. user_data: lowered.user_data,
  78. samplers: lowered.samplers,
  79. static_samplers: Vec::new(),
  80. root_cbvs: lowered.root_cbvs,
  81. root_srvs: lowered.root_srvs,
  82. root_uavs: lowered.root_uavs,
  83. root_indexed_views: Vec::new(),
  84. };
  85. if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
  86. v.validate(&mut shader.dxil.clone())
  87. .expect("DXIL validation failed");
  88. } else {
  89. eprintln!("no dxil.dll; validation skipped");
  90. }
  91. shader
  92. }
  93. /// Runs `shader` with `views` at heap indices 0.. (the UAV last) on the
  94. /// hardware GPU and on WARP (each skipped without a device) and returns,
  95. /// per one that ran, the UAV's 32 dwords.
  96. pub(super) fn run(shader: &HostShader, views: &[View]) -> Vec<(&'static str, Vec<u32>)> {
  97. let samplers = [D3D12_SAMPLER_DESC {
  98. Filter: D3D12_FILTER_MIN_MAG_MIP_POINT,
  99. AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
  100. AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
  101. AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
  102. MaxAnisotropy: 1,
  103. MaxLOD: f32::MAX,
  104. ..Default::default()
  105. }];
  106. let mut out = Vec::new();
  107. // One GPU test at a time, with the VALU tests: in parallel on the
  108. // hardware adapter and WARP the test process sometimes died of an
  109. // access violation (7 of 24 runs of these four tests).
  110. let _one = super::valu_tests::ONE_AT_A_TIME
  111. .lock()
  112. .unwrap_or_else(|e| e.into_inner());
  113. for (label, warp) in [("hardware", false), ("warp", true)] {
  114. let Ok(host) = Host::new(warp) else {
  115. eprintln!("no SM 6.6 {label} device; skipped");
  116. continue;
  117. };
  118. let (root, pso) = match host.compute_pipeline(shader) {
  119. Ok(p) => p,
  120. Err(e) => panic!("{label}: compute_pipeline failed: {e}"),
  121. };
  122. // Both tables start at heap index 0.
  123. let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
  124. for u in &shader.user_data {
  125. assert_eq!(u.root, RootType::Table, "{u:?}");
  126. ud[u.index as usize] = 0;
  127. }
  128. let words = host
  129. .dispatch(
  130. &root,
  131. &pso,
  132. &Dispatch {
  133. roots: vec![(0, Root::Constants(ud))],
  134. views,
  135. samplers: &samplers,
  136. groups: [1, 1, 1],
  137. },
  138. )
  139. .unwrap_or_else(|e| panic!("{label}: dispatch failed: {e}"));
  140. out.push((label, words[0].clone()));
  141. }
  142. out
  143. }
  144. /// The red channel each lane sampled from two 1x1 textures, red 1.0 at
  145. /// heap index 0 and 2.0 at 1.
  146. fn sampled(shader: &HostShader) -> Vec<(&'static str, Vec<f32>)> {
  147. let first = [[1.0f32, 0.0, 0.0, 1.0]];
  148. let second = [[2.0f32, 0.0, 0.0, 1.0]];
  149. let views = [
  150. View::Texture(1, 1, &first),
  151. View::Texture(1, 1, &second),
  152. View::RawUav(32),
  153. ];
  154. run(shader, &views)
  155. .into_iter()
  156. .map(|(label, words)| (label, words.into_iter().map(f32::from_bits).collect()))
  157. .collect()
  158. }
  159. #[test]
  160. fn lanes_sample_the_texture_their_if_else_arm_chose() {
  161. use crate::resource::tests::WATERFALL_JOINED_WAVE32;
  162. let shader = build(WATERFALL_JOINED_WAVE32, false);
  163. for (label, red) in sampled(&shader) {
  164. // Lanes below 16 took the first texture, the others the second.
  165. let want: Vec<f32> = (0..32).map(|l| if l < 16 { 1.0 } else { 2.0 }).collect();
  166. assert_eq!(red, want, "{label}");
  167. }
  168. }
  169. #[test]
  170. fn lanes_sample_the_texture_their_arm_loaded_or_picked() {
  171. use crate::resource::tests::WATERFALL_JOINED_MIXED_WAVE32;
  172. let shader = build(WATERFALL_JOINED_MIXED_WAVE32, false);
  173. for (label, red) in sampled(&shader) {
  174. // Lanes below 16 loaded entry (lane & 1); of the others, lanes
  175. // below 24 picked the first, the rest the second.
  176. let want: Vec<f32> = (0..32)
  177. .map(|l| match l {
  178. 0..16 => [1.0, 2.0][l & 1],
  179. 16..24 => 1.0,
  180. _ => 2.0,
  181. })
  182. .collect();
  183. assert_eq!(red, want, "{label}");
  184. }
  185. }
  186. #[test]
  187. fn lanes_load_from_the_buffer_their_nested_arms_chose() {
  188. use crate::resource::tests::WATERFALL_JOINED_BUFFER_WAVE32;
  189. let shader = build(WATERFALL_JOINED_BUFFER_WAVE32, true);
  190. let first: Vec<u32> = (0..32).map(|i| 100 + i).collect();
  191. let second: Vec<u32> = (0..32).map(|i| 200 + i).collect();
  192. let views = [
  193. View::Buffer {
  194. data: &first,
  195. stride: Stride::Structured(4),
  196. },
  197. View::Buffer {
  198. data: &second,
  199. stride: Stride::Structured(4),
  200. },
  201. View::RawUav(32),
  202. ];
  203. for (label, got) in run(&shader, &views) {
  204. // Dword `lane` of the first buffer below 16, of the second from 16
  205. // to 23; lanes from 24 on load and store nothing.
  206. let want: Vec<u32> = (0..32)
  207. .map(|l| match l {
  208. 0..16 => 100 + l,
  209. 16..24 => 200 + l,
  210. _ => 0,
  211. })
  212. .collect();
  213. assert_eq!(got, want, "{label}");
  214. }
  215. }
  216. #[test]
  217. fn lanes_sample_the_entry_their_offset_names_in_the_console_waterfall() {
  218. use crate::resource::tests::WATERFALL_INDEXED_WAVE32;
  219. let shader = build(WATERFALL_INDEXED_WAVE32, false);
  220. let results = sampled(&shader);
  221. assert!(!results.is_empty());
  222. for (label, red) in results {
  223. // Even lanes hold entry 0's offset, odd lanes entry 1's.
  224. let want: Vec<f32> = (0..32).map(|l| [1.0, 2.0][l & 1]).collect();
  225. assert_eq!(red, want, "{label}");
  226. }
  227. }
  228. #[test]
  229. fn lanes_sample_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx() {
  230. use crate::resource::tests::cmpx_waterfall;
  231. // Lanes below 16 hold the first descriptor, the others the second.
  232. let want: Vec<f32> = (0..32).map(|l| if l < 16 { 1.0 } else { 2.0 }).collect();
  233. for folded in [false, true] {
  234. let shader = build(&cmpx_waterfall(false, folded), false);
  235. let results = sampled(&shader);
  236. assert!(!results.is_empty());
  237. for (label, red) in results {
  238. assert_eq!(red, want, "{label}, folded {folded}");
  239. }
  240. }
  241. }
  242. #[test]
  243. fn immediate_sampler_chosen_by_control_flow() {
  244. let s_mov_lit = |dst: u8, lit: u32| [0xBE80_03FF | (dst as u32) << 16, lit];
  245. let s_mov_zero = |dst: u8| 0xBE80_0380 | (dst as u32) << 16;
  246. let s_load_x8 = |dst: u8, base: u8| [0x3D << 26 | 0x03 << 18 | (dst as u32) << 6 | (base as u32 >> 1), 125 << 25];
  247. let s_load_x4_off = |dst: u8, base: u8, off: u32| [0x3D << 26 | 0x02 << 18 | (dst as u32) << 6 | (base as u32 >> 1), 125 << 25 | off];
  248. // image_sample_lz v4, v[2:3], s[8:15], s[16:19] dmask:0x1 dim:SQ_RSRC_IMG_2D
  249. let image_sample_lz: [u32; 2] = [
  250. 0x3C << 26 | 0x21 << 18 | 1 << 8 | 1 << 3, // dmask=1, dim=2D(1)
  251. (16u32 >> 2) << 21 | (8u32 >> 2) << 16 | 4 << 8 | 2,
  252. ];
  253. // buffer_store_dword v4, off, s[20:23], 0
  254. let buf_store: [u32; 2] = [0x38 << 26 | 0x1C << 18, 0x80 << 24 | (20u32 >> 2) << 16 | 4 << 8];
  255. // Two distinct valid immediate sampler descriptors (different wrap/filter bits).
  256. let samp_a: [u32; 4] = [0x0000_0092, 0x00C0_0000, 0x1850_0000, 0];
  257. let samp_b: [u32; 4] = [0x0000_0092, 0x00C0_0000, 0x1850_0001, 0];
  258. for (case_name, alt) in [("same_sampler", samp_a), ("different_samplers", samp_b)] {
  259. let mut words = Vec::new();
  260. // s[2:3] = view table base (s0, 0).
  261. words.push(0xBE80_0300 | (2 << 16)); // s_mov_b32 s2, s0
  262. words.push(s_mov_zero(3));
  263. words.extend_from_slice(&s_load_x8(8, 2)); // s[8:15] = texture SRV (offset 0)
  264. words.extend_from_slice(&s_load_x4_off(20, 2, 64)); // s[20:23] = raw UAV (offset 16 dwords = 64B)
  265. // v2 = 0.5, v3 = 0.5 (sample coordinates).
  266. words.push(0x7E00_02F0 | (2 << 17)); // v_mov_b32 v2, 0.5
  267. words.push(0x7E00_02F0 | (3 << 17)); // v_mov_b32 v3, 0.5
  268. // s_cmp_eq_u32 s1, 0; s_cbranch_scc0 else_arm
  269. words.push(0xBF00_8001); // s_cmp_eq_u32 s1, 0 (const 128)
  270. let br_else = words.len();
  271. words.push(0); // placeholder s_cbranch_scc0
  272. // then arm: load samp_a into s[16:19]
  273. for (i, &w) in samp_a.iter().enumerate() {
  274. words.extend_from_slice(&s_mov_lit(16 + i as u8, w));
  275. }
  276. let br_join = words.len();
  277. words.push(0); // placeholder s_branch
  278. let else_pc = words.len();
  279. // else arm: load alt into s[16:19]
  280. for (i, &w) in alt.iter().enumerate() {
  281. words.extend_from_slice(&s_mov_lit(16 + i as u8, w));
  282. }
  283. let join_pc = words.len();
  284. words[br_else] = 0xBF84_0000 | ((else_pc - br_else - 1) as u32 & 0xFFFF);
  285. words[br_join] = 0xBF82_0000 | ((join_pc - br_join - 1) as u32 & 0xFFFF);
  286. words.extend_from_slice(&image_sample_lz);
  287. words.extend_from_slice(&buf_store);
  288. words.push(0xBF81_0000); // s_endpgm
  289. let native = NativeProgram {
  290. stage: HwStage::Cs,
  291. wave64: false,
  292. vgprs: 8,
  293. layout: vec![
  294. RootParam {
  295. root: RootType::Table,
  296. table_offset_dwords: 0,
  297. sgprs: 1,
  298. base_register: 0,
  299. user_data: 0,
  300. slot: 0,
  301. },
  302. RootParam {
  303. root: RootType::Constants,
  304. table_offset_dwords: 0,
  305. sgprs: 1,
  306. base_register: 0,
  307. user_data: 1,
  308. slot: 1,
  309. },
  310. ],
  311. used: vec![
  312. UsedResource {
  313. kind: ResourceType::Srv,
  314. root: RootType::Table,
  315. table_offset_dwords: 0,
  316. lower_bound: 0,
  317. stride: 0,
  318. dwords: 8,
  319. user_data: 0,
  320. slot: 0,
  321. },
  322. UsedResource {
  323. kind: ResourceType::Uav,
  324. root: RootType::Table,
  325. table_offset_dwords: 16,
  326. lower_bound: 0,
  327. stride: 0,
  328. dwords: 4,
  329. user_data: 0,
  330. slot: 0,
  331. },
  332. ],
  333. io: StageIo::Cs(ComputeIo {
  334. rsrc2: 2 << 1,
  335. threads: [1, 1, 1],
  336. }),
  337. code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
  338. };
  339. let p = ir::build(&native.code, false, native.vgprs).expect("ir::build");
  340. let st = structure::structure(&p).expect("structure");
  341. let res = resource::track(&p, &native.layout, native.stage, Some(&st)).expect("track");
  342. let io = io::raise(&p, &native).expect("io::raise");
  343. let lowered = lower::lower(&lower::Parts {
  344. native: &native,
  345. p: &p,
  346. st: &st,
  347. io: &io,
  348. res: &res,
  349. inputs: Vec::new(),
  350. outputs: Vec::new(),
  351. prims: Vec::new(),
  352. mesh_stride: 0,
  353. })
  354. .unwrap_or_else(|e| panic!("{case_name}: lower failed: {e}"));
  355. let static_samplers = lowered
  356. .samplers
  357. .iter()
  358. .map(|&w| super::StaticSampler::from_words(w))
  359. .collect();
  360. let shader = HostShader {
  361. stage: HwStage::Cs,
  362. dxil: lowered.dxil,
  363. user_data: lowered.user_data,
  364. samplers: lowered.samplers,
  365. static_samplers,
  366. root_cbvs: lowered.root_cbvs,
  367. root_srvs: lowered.root_srvs,
  368. root_uavs: lowered.root_uavs,
  369. root_indexed_views: Vec::new(),
  370. };
  371. if let Ok(mut v) =
  372. validator::Validator::load(validator::Validator::default_path().as_deref())
  373. {
  374. let mut copy = shader.dxil.clone();
  375. v.validate(&mut copy).expect("DXIL validation failed");
  376. }
  377. let _one = super::valu_tests::ONE_AT_A_TIME
  378. .lock()
  379. .unwrap_or_else(|e| e.into_inner());
  380. for (label, warp) in [("hardware", false), ("warp", true)] {
  381. let Ok(host) = Host::new(warp) else {
  382. continue;
  383. };
  384. let (root, pso) = host
  385. .compute_pipeline(&shader)
  386. .unwrap_or_else(|e| panic!("{case_name}/{label}: compute_pipeline failed: {e}"));
  387. for branch in [0u32, 1u32] {
  388. let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
  389. for u in &shader.user_data {
  390. ud[u.index as usize] = match (u.root, u.slot) {
  391. (RootType::Table, 0) => 0,
  392. (RootType::Constants, 1) => branch,
  393. other => panic!("unexpected user data {other:?}"),
  394. };
  395. }
  396. let texels = [[1.0f32, 0.0, 0.0, 1.0]];
  397. let out = host
  398. .dispatch(
  399. &root,
  400. &pso,
  401. &Dispatch {
  402. roots: vec![(0, Root::Constants(ud))],
  403. views: &[
  404. View::Texture(1, 1, &texels),
  405. View::Texture(1, 1, &texels),
  406. View::RawUav(1),
  407. ],
  408. samplers: &[],
  409. groups: [1, 1, 1],
  410. },
  411. )
  412. .unwrap_or_else(|e| panic!("{case_name}/{label}: dispatch failed: {e}"));
  413. let got = f32::from_bits(out[0][0]);
  414. assert_eq!(got, 1.0, "{case_name}/{label} branch={branch}");
  415. }
  416. }
  417. }
  418. }