size_tests.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. //! End-to-end tests of what a program tells about a view by how it uses it:
  2. //! descriptors read for sizes (the dwords of a view no instruction uses) and
  3. //! counters, lowered to DXIL, validated by `dxil.dll` when present, and run
  4. //! on the host GPU and WARP.
  5. use super::host::{Stride, View};
  6. use super::waterfall_tests::run;
  7. use super::{HostShader, lower, validator};
  8. use crate::native::{
  9. ComputeIo, HwStage, NativeProgram, ResourceType, RootParam, RootType, StageIo, UsedResource,
  10. };
  11. use crate::{ShaderError, io, ir, resource, structure};
  12. /// Lowers a wave32 compute program of one thread with the table of views at
  13. /// s0 and the view heap at s1; `srv`: the dwords and the stride of the used
  14. /// resource at the table's first entry (the third is a raw UAV).
  15. fn build(words: &[u32], srv: (u8, u32)) -> Result<HostShader, ShaderError> {
  16. let param = |root, user_data| RootParam {
  17. root,
  18. table_offset_dwords: 0,
  19. sgprs: 1,
  20. base_register: 0,
  21. user_data,
  22. slot: 0,
  23. };
  24. let range = |kind, table_offset_dwords, dwords, stride| UsedResource {
  25. kind,
  26. root: RootType::Table,
  27. table_offset_dwords,
  28. lower_bound: 0,
  29. stride,
  30. dwords,
  31. user_data: 0,
  32. slot: 0,
  33. };
  34. let native = NativeProgram {
  35. stage: HwStage::Cs,
  36. wave64: false,
  37. vgprs: 32,
  38. layout: vec![param(RootType::Table, 0), param(RootType::ResourceHeap, 1)],
  39. used: vec![
  40. range(ResourceType::Srv, 0, srv.0, srv.1),
  41. range(ResourceType::Uav, 16, 4, 0),
  42. ],
  43. io: StageIo::Cs(ComputeIo {
  44. rsrc2: 2 << 1,
  45. threads: [1, 1, 1],
  46. }),
  47. code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
  48. };
  49. let p = ir::build(&native.code, native.wave64, native.vgprs)?;
  50. let st = structure::structure(&p)?;
  51. let res = resource::track(&p, &native.layout, native.stage, Some(&st))?;
  52. let io = io::raise(&p, &native)?;
  53. let lowered = lower::lower(&lower::Parts {
  54. native: &native,
  55. p: &p,
  56. st: &st,
  57. io: &io,
  58. res: &res,
  59. inputs: Vec::new(),
  60. outputs: Vec::new(),
  61. prims: Vec::new(),
  62. mesh_stride: 0,
  63. })?;
  64. let shader = HostShader {
  65. stage: HwStage::Cs,
  66. dxil: lowered.dxil,
  67. user_data: lowered.user_data,
  68. samplers: lowered.samplers,
  69. static_samplers: Vec::new(),
  70. root_cbvs: lowered.root_cbvs,
  71. root_srvs: lowered.root_srvs,
  72. root_uavs: lowered.root_uavs,
  73. root_indexed_views: Vec::new(),
  74. };
  75. if let Ok(mut v) = validator::Validator::load(validator::Validator::default_path().as_deref()) {
  76. v.validate(&mut shader.dxil.clone())
  77. .expect("DXIL validation failed");
  78. } else {
  79. eprintln!("no dxil.dll; validation skipped");
  80. }
  81. Ok(shader)
  82. }
  83. /// The table at s[2:3], its raw UAV's descriptor in s[20:23].
  84. const TABLE: &[u32] = &[
  85. 0xBE82_0300, // s_mov_b32 s2, s0
  86. 0xBE83_0380, // s_mov_b32 s3, 0
  87. 0xF408_0501, // s_load_dwordx4 s[20:23], s[2:3], 64
  88. 0xFA00_0040,
  89. ];
  90. /// Stores s4 and s5 at bytes 0 and 4 of the UAV.
  91. const STORE: &[u32] = &[
  92. 0x7E08_0204, // v_mov_b32 v4, s4
  93. 0x7E0A_0205, // v_mov_b32 v5, s5
  94. 0xE070_0000, // buffer_store_dword v4, off, s[20:23], 0
  95. 0x8005_0400,
  96. 0xE070_0004, // buffer_store_dword v5, off, s[20:23], 0 offset:4
  97. 0x8005_0500,
  98. 0xBF81_0000, // s_endpgm
  99. ];
  100. /// `GetDimensions` of a texture as the console compiler expands it, from
  101. /// the descriptor in s[8:15]: width in s4, height in s5, base level in s6.
  102. const SIZES: &[u32] = &[
  103. 0x9004_9E09, // s_lshr_b32 s4, s9, 30
  104. 0x8F05_820A, // s_lshl_b32 s5, s10, 2
  105. 0x8804_0504, // s_or_b32 s4, s4, s5
  106. 0x8704_FF04, // s_and_b32 s4, s4, 0xffff
  107. 0x0000_FFFF,
  108. 0x9385_FF0A, // s_bfe_u32 s5, s10, 0x10000e
  109. 0x0010_000E,
  110. 0x9386_FF0B, // s_bfe_u32 s6, s11, 0x4000c
  111. 0x0004_000C,
  112. 0x8104_8104, // s_add_i32 s4, s4, 1
  113. 0x8105_8105, // s_add_i32 s5, s5, 1
  114. 0x9004_0604, // s_lshr_b32 s4, s4, s6
  115. 0x9005_0605, // s_lshr_b32 s5, s5, s6
  116. ];
  117. /// s_load_dwordx8 s[8:15], s[6:7], s12: entry 1 of the view heap.
  118. const FROM_HEAP: &[u32] = &[
  119. 0xBE86_0301, // s_mov_b32 s6, s1
  120. 0xBE87_0380, // s_mov_b32 s7, 0
  121. 0xBE8C_03A0, // s_mov_b32 s12, 32
  122. 0xF40C_0203,
  123. 0x1800_0000,
  124. ];
  125. /// `s_load_dwordx4 s[8:11], s[2:3], 0`, then the record count in s4 and 0
  126. /// in s5.
  127. const COUNT: &[u32] = &[
  128. 0xF408_0201,
  129. 0xFA00_0000,
  130. 0xBE84_030A, // s_mov_b32 s4, s10
  131. 0xBE85_0380, // s_mov_b32 s5, 0
  132. ];
  133. /// A puddle pass of the title picks three textures from the view heap,
  134. /// reads the first one's size and samples the other two.
  135. #[test]
  136. fn the_size_of_a_heap_texture_nothing_reads() {
  137. let words = [TABLE, FROM_HEAP, SIZES, STORE].concat();
  138. let shader = build(&words, (8, 0)).expect("translates");
  139. let (one, texels) = ([[0.0; 4]; 1], [[0.0; 4]; 15]);
  140. let views = [
  141. View::Texture(1, 1, &one),
  142. View::Texture(5, 3, &texels),
  143. View::RawUav(2),
  144. ];
  145. for (label, got) in run(&shader, &views) {
  146. assert_eq!(got[..2], [5, 3], "{label}");
  147. }
  148. }
  149. /// Without the height nothing tells a 2D texture from the others: taken for
  150. /// one, as most are. A volume's depth or an array's layers are no 2D
  151. /// texture's.
  152. #[test]
  153. fn sizes_of_a_volume_or_an_array_stay_untranslated() {
  154. let width = [
  155. 0x9004_9E09, // s_lshr_b32 s4, s9, 30
  156. 0x8F05_820A, // s_lshl_b32 s5, s10, 2
  157. 0x8804_0504, // s_or_b32 s4, s4, s5
  158. ];
  159. let words = [TABLE, FROM_HEAP, &width, STORE].concat();
  160. assert!(build(&words, (8, 0)).is_ok());
  161. let depth = [
  162. 0x8805_0C05, // s_or_b32 s5, s5, s12
  163. ];
  164. let words = [TABLE, FROM_HEAP, SIZES, &depth, STORE].concat();
  165. assert!(build(&words, (8, 0)).is_err());
  166. }
  167. /// A water simulation step runs only while a structured buffer is bound:
  168. /// it tests the record count of a view it never reads.
  169. #[test]
  170. fn the_record_count_of_a_structured_buffer_nothing_reads() {
  171. let words = [TABLE, COUNT, STORE].concat();
  172. let shader = build(&words, (4, 16)).expect("translates");
  173. let (data, texel) = ([0u32; 12], [[0.0; 4]; 1]);
  174. // The runtime makes structured views raw.
  175. let views = [
  176. View::Buffer {
  177. data: &data,
  178. stride: Stride::Raw,
  179. },
  180. View::Texture(1, 1, &texel),
  181. View::RawUav(2),
  182. ];
  183. for (label, got) in run(&shader, &views) {
  184. assert_eq!(got[..2], [3, 0], "{label}");
  185. }
  186. // A stride of 4 or less may be a typed view's component count.
  187. assert!(build(&words, (4, 4)).is_err());
  188. assert!(build(&words, (4, 0)).is_err());
  189. }
  190. /// As [`the_record_count_of_a_structured_buffer_nothing_reads`], of one the
  191. /// program also reads by index.
  192. #[test]
  193. fn the_record_count_of_a_structured_buffer_read_by_index() {
  194. let load = [
  195. 0x7E06_0280, // v_mov_b32 v3, 0
  196. 0xE030_2000, // buffer_load_dword v5, v3, s[8:11], 0 idxen
  197. 0x8002_0503,
  198. 0x7E0A_0505, // v_readfirstlane_b32 s5, v5
  199. ];
  200. let words = [TABLE, COUNT, &load, STORE].concat();
  201. let shader = build(&words, (4, 16)).expect("translates");
  202. let (mut data, texel) = ([0u32; 12], [[0.0; 4]; 1]);
  203. data[0] = 77;
  204. let views = [
  205. View::Buffer {
  206. data: &data,
  207. stride: Stride::Raw,
  208. },
  209. View::Texture(1, 1, &texel),
  210. View::RawUav(2),
  211. ];
  212. for (label, got) in run(&shader, &views) {
  213. assert_eq!(got[..2], [3, 77], "{label}");
  214. }
  215. }
  216. /// An upscaler counts its groups in a texture with atomics and sets the
  217. /// counter back with a store of one channel, 16 bits wide.
  218. #[test]
  219. fn a_counter_texture_is_set_by_a_store_of_one_channel() {
  220. let program = |store: [u32; 2], value: u32| {
  221. let words = [
  222. 0xF40C_0201u32, // s_load_dwordx8 s[8:15], s[2:3], 0
  223. 0xFA00_0000,
  224. 0x7E00_0280, // v_mov_b32 v0, 0
  225. 0x7E02_0280, // v_mov_b32 v1, 0
  226. 0x7E04_0283, // v_mov_b32 v2, 3
  227. 0xF044_3108, // image_atomic_add v2, v[0:1], s[8:15] dmask:0x1 2D glc
  228. 0x0002_0200,
  229. value, // v_mov_b32 v3, ..
  230. store[0],
  231. store[1],
  232. 0x7E08_0281, // v_mov_b32 v4, 1
  233. 0xF044_3108, // image_atomic_add v4, v[0:1], s[8:15] dmask:0x1 2D glc
  234. 0x0002_0400,
  235. 0xBE84_0380, // s_mov_b32 s4, 0
  236. 0xBE85_0380, // s_mov_b32 s5, 0
  237. ];
  238. build(&[TABLE, &words, STORE].concat(), (8, 0))
  239. };
  240. // image_store v3, v[0:1], s[8:15] dmask:0x1 2D, with and without d16
  241. for store in [[0xF020_1108, 0x0002_0300], [0xF020_1108, 0x8002_0300]] {
  242. let shader = program(store, 0x7E06_0289).expect("translates"); // v3 = 9
  243. let texel = [[0.0; 4]; 1];
  244. let views = [
  245. View::UintTextureUav(1, 1),
  246. View::Texture(1, 1, &texel),
  247. View::RawUav(2),
  248. ];
  249. for (label, got) in run(&shader, &views) {
  250. assert_eq!(got[0], 10, "{label}");
  251. }
  252. }
  253. // Sixteen bits of a number only the card knows may be signed.
  254. assert!(program([0xF020_1108, 0x8002_0300], 0x7E06_0302).is_err()); // v3 = v2
  255. // A store of two channels says nothing about the view.
  256. assert!(program([0xF020_1308, 0x0002_0300], 0x7E06_0289).is_err());
  257. }
  258. /// A load past the wave's cache (GLC) of a buffer other groups write: its
  259. /// view is globally coherent on the host, as is every access to it.
  260. #[test]
  261. fn a_buffer_loaded_past_the_cache_is_coherent() {
  262. let program = |load: u32| {
  263. let words = [
  264. 0x7E04_028Bu32, // v_mov_b32 v2, 11
  265. 0xE070_0008, // buffer_store_dword v2, off, s[20:23], 0 offset:8
  266. 0x8005_0200,
  267. load, // buffer_load_dword v3, off, s[20:23], 0 offset:8
  268. 0x8005_0300,
  269. 0x7E08_0503, // v_readfirstlane_b32 s4, v3
  270. 0xBE85_0380, // s_mov_b32 s5, 0
  271. ];
  272. build(&[TABLE, &words, STORE].concat(), (8, 0)).expect("translates")
  273. };
  274. let (plain, coherent) = (program(0xE030_0008), program(0xE030_C008));
  275. // the annotations of the store's and the load's handles
  276. assert_ne!(plain.dxil, coherent.dxil);
  277. let texel = [[0.0; 4]; 1];
  278. let views = [
  279. View::Texture(1, 1, &texel),
  280. View::Texture(1, 1, &texel),
  281. View::RawUav(3),
  282. ];
  283. for shader in [&plain, &coherent] {
  284. for (label, got) in run(shader, &views) {
  285. assert_eq!(got[..3], [11, 0, 11], "{label}");
  286. }
  287. }
  288. }
  289. /// A buffer picked from the view heap and read as raw bytes is read at the
  290. /// host's raw twin of its slot, when the host keeps some; an index past the
  291. /// title's slots reads the last twin, not a slot of something else.
  292. #[test]
  293. fn a_picked_buffer_is_read_at_its_raw_twin() {
  294. let program = |at: u32| {
  295. let words = [
  296. 0xBE86_0301, // s_mov_b32 s6, s1
  297. 0xBE87_0380, // s_mov_b32 s7, 0
  298. 0xBE8C_03FF, // s_mov_b32 s12, at
  299. at,
  300. 0xF408_0203, // s_load_dwordx4 s[8:11], s[6:7], s12
  301. 0x1800_0000,
  302. 0xE030_0004, // buffer_load_dword v5, off, s[8:11], 0 offset:4
  303. 0x8002_0500,
  304. 0x7E0A_0505, // v_readfirstlane_b32 s5, v5
  305. 0xBE84_0380, // s_mov_b32 s4, 0
  306. ];
  307. [TABLE, &words, STORE].concat()
  308. };
  309. let (title, twin, last, texel) = ([1u32, 2, 3, 4], [5u32, 6, 7, 8], [9u32, 10, 11, 12], [[0.0; 4]; 1]);
  310. let raw = |data| View::Buffer { data, stride: Stride::Raw };
  311. // (The store goes through the table's third entry: its twin, too.)
  312. let views = |twins: bool| {
  313. let uav = |here: bool| if here { View::RawUav(2) } else { View::Texture(1, 1, &texel) };
  314. [
  315. View::Texture(1, 1, &texel),
  316. raw(&title),
  317. uav(!twins),
  318. View::Texture(1, 1, &texel),
  319. View::Texture(1, 1, &texel),
  320. raw(&twin),
  321. uav(twins),
  322. raw(&last),
  323. ]
  324. };
  325. for (first, at, expected) in [(0, 32, 2), (4, 32, 6), (4, 32 * 1000, 10)] {
  326. super::shader::RAW_VIEWS_HERE.set(Some(first));
  327. let shader = build(&program(at), (8, 0)).expect("translates");
  328. super::shader::RAW_VIEWS_HERE.set(None);
  329. for (label, got) in run(&shader, &views(first != 0)) {
  330. assert_eq!(got[..2], [0, expected], "{label}: twins from {first}, byte {at}");
  331. }
  332. }
  333. }
  334. /// A table's buffer read as raw bytes is read at its raw twin too.
  335. #[test]
  336. fn a_tables_raw_buffer_is_read_at_its_raw_twin() {
  337. let load = [
  338. 0xF408_0201u32, // s_load_dwordx4 s[8:11], s[2:3], 0
  339. 0xFA00_0000,
  340. 0xE030_0004, // buffer_load_dword v5, off, s[8:11], 0 offset:4
  341. 0x8002_0500,
  342. 0x7E0A_0505, // v_readfirstlane_b32 s5, v5
  343. 0xBE84_0380, // s_mov_b32 s4, 0
  344. ];
  345. let words = [TABLE, &load, STORE].concat();
  346. let (title, twin, texel) = ([1u32, 2, 3, 4], [5u32, 6, 7, 8], [[0.0; 4]; 1]);
  347. let raw = |data| View::Buffer { data, stride: Stride::Raw };
  348. super::shader::RAW_VIEWS_HERE.set(Some(4));
  349. let shader = build(&words, (4, 0)).expect("translates");
  350. super::shader::RAW_VIEWS_HERE.set(None);
  351. let other = || View::Texture(1, 1, &texel);
  352. let views = [raw(&title), other(), other(), other(), raw(&twin), other(), View::RawUav(2)];
  353. for (label, got) in run(&shader, &views) {
  354. assert_eq!(got[..2], [0, 6], "{label}");
  355. }
  356. }