Shaders: lower immediate samplers chosen by uniform control flow
This commit is contained in:
1 parent
16849898f3
commit
cb80bf0f9c
2 files changed
+381
-61
No files matched your search
@@ -249,3 +249,187 @@ fn lanes_sample_their_own_texture_in_a_waterfall_narrowed_by_v_cmpx() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn immediate_sampler_chosen_by_control_flow() {
|
||||
let s_mov_lit = |dst: u8, lit: u32| [0xBE80_03FF | (dst as u32) << 16, lit];
|
||||
let s_mov_zero = |dst: u8| 0xBE80_0380 | (dst as u32) << 16;
|
||||
let s_load_x8 = |dst: u8, base: u8| [0x3D << 26 | 0x03 << 18 | (dst as u32) << 6 | (base as u32 >> 1), 125 << 25];
|
||||
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];
|
||||
// image_sample_lz v4, v[2:3], s[8:15], s[16:19] dmask:0x1 dim:SQ_RSRC_IMG_2D
|
||||
let image_sample_lz: [u32; 2] = [
|
||||
0x3C << 26 | 0x21 << 18 | 1 << 8 | 1 << 3, // dmask=1, dim=2D(1)
|
||||
(16u32 >> 2) << 21 | (8u32 >> 2) << 16 | 4 << 8 | 2,
|
||||
];
|
||||
// buffer_store_dword v4, off, s[20:23], 0
|
||||
let buf_store: [u32; 2] = [0x38 << 26 | 0x1C << 18, 0x80 << 24 | (20u32 >> 2) << 16 | 4 << 8];
|
||||
|
||||
// Two distinct valid immediate sampler descriptors (different wrap/filter bits).
|
||||
let samp_a: [u32; 4] = [0x0000_0092, 0x00C0_0000, 0x1850_0000, 0];
|
||||
let samp_b: [u32; 4] = [0x0000_0092, 0x00C0_0000, 0x1850_0001, 0];
|
||||
|
||||
for (case_name, alt) in [("same_sampler", samp_a), ("different_samplers", samp_b)] {
|
||||
let mut words = Vec::new();
|
||||
// s[2:3] = view table base (s0, 0).
|
||||
words.push(0xBE80_0300 | (2 << 16)); // s_mov_b32 s2, s0
|
||||
words.push(s_mov_zero(3));
|
||||
words.extend_from_slice(&s_load_x8(8, 2)); // s[8:15] = texture SRV (offset 0)
|
||||
words.extend_from_slice(&s_load_x4_off(20, 2, 64)); // s[20:23] = raw UAV (offset 16 dwords = 64B)
|
||||
// v2 = 0.5, v3 = 0.5 (sample coordinates).
|
||||
words.push(0x7E00_02F0 | (2 << 17)); // v_mov_b32 v2, 0.5
|
||||
words.push(0x7E00_02F0 | (3 << 17)); // v_mov_b32 v3, 0.5
|
||||
// s_cmp_eq_u32 s1, 0; s_cbranch_scc0 else_arm
|
||||
words.push(0xBF00_8001); // s_cmp_eq_u32 s1, 0 (const 128)
|
||||
let br_else = words.len();
|
||||
words.push(0); // placeholder s_cbranch_scc0
|
||||
// then arm: load samp_a into s[16:19]
|
||||
for (i, &w) in samp_a.iter().enumerate() {
|
||||
words.extend_from_slice(&s_mov_lit(16 + i as u8, w));
|
||||
}
|
||||
let br_join = words.len();
|
||||
words.push(0); // placeholder s_branch
|
||||
let else_pc = words.len();
|
||||
// else arm: load alt into s[16:19]
|
||||
for (i, &w) in alt.iter().enumerate() {
|
||||
words.extend_from_slice(&s_mov_lit(16 + i as u8, w));
|
||||
}
|
||||
let join_pc = words.len();
|
||||
words[br_else] = 0xBF84_0000 | ((else_pc - br_else - 1) as u32 & 0xFFFF);
|
||||
words[br_join] = 0xBF82_0000 | ((join_pc - br_join - 1) as u32 & 0xFFFF);
|
||||
words.extend_from_slice(&image_sample_lz);
|
||||
words.extend_from_slice(&buf_store);
|
||||
words.push(0xBF81_0000); // s_endpgm
|
||||
|
||||
let native = NativeProgram {
|
||||
stage: HwStage::Cs,
|
||||
wave64: false,
|
||||
vgprs: 8,
|
||||
layout: vec![
|
||||
RootParam {
|
||||
root: RootType::Table,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 1,
|
||||
base_register: 0,
|
||||
user_data: 0,
|
||||
slot: 0,
|
||||
},
|
||||
RootParam {
|
||||
root: RootType::Constants,
|
||||
table_offset_dwords: 0,
|
||||
sgprs: 1,
|
||||
base_register: 0,
|
||||
user_data: 1,
|
||||
slot: 1,
|
||||
},
|
||||
],
|
||||
used: vec![
|
||||
UsedResource {
|
||||
kind: ResourceType::Srv,
|
||||
root: RootType::Table,
|
||||
table_offset_dwords: 0,
|
||||
lower_bound: 0,
|
||||
stride: 0,
|
||||
dwords: 8,
|
||||
user_data: 0,
|
||||
slot: 0,
|
||||
},
|
||||
UsedResource {
|
||||
kind: ResourceType::Uav,
|
||||
root: RootType::Table,
|
||||
table_offset_dwords: 16,
|
||||
lower_bound: 0,
|
||||
stride: 0,
|
||||
dwords: 4,
|
||||
user_data: 0,
|
||||
slot: 0,
|
||||
},
|
||||
],
|
||||
io: StageIo::Cs(ComputeIo {
|
||||
rsrc2: 2 << 1,
|
||||
threads: [1, 1, 1],
|
||||
}),
|
||||
code: words.iter().flat_map(|w| w.to_le_bytes()).collect(),
|
||||
};
|
||||
|
||||
let p = ir::build(&native.code, false, native.vgprs).expect("ir::build");
|
||||
let st = structure::structure(&p).expect("structure");
|
||||
let res = resource::track(&p, &native.layout, native.stage, Some(&st)).expect("track");
|
||||
let io = io::raise(&p, &native).expect("io::raise");
|
||||
let lowered = lower::lower(&lower::Parts {
|
||||
native: &native,
|
||||
p: &p,
|
||||
st: &st,
|
||||
io: &io,
|
||||
res: &res,
|
||||
inputs: Vec::new(),
|
||||
outputs: Vec::new(),
|
||||
prims: Vec::new(),
|
||||
mesh_stride: 0,
|
||||
})
|
||||
.unwrap_or_else(|e| panic!("{case_name}: lower failed: {e}"));
|
||||
|
||||
let static_samplers = lowered
|
||||
.samplers
|
||||
.iter()
|
||||
.map(|&w| super::StaticSampler::from_words(w))
|
||||
.collect();
|
||||
let shader = HostShader {
|
||||
stage: HwStage::Cs,
|
||||
dxil: lowered.dxil,
|
||||
user_data: lowered.user_data,
|
||||
samplers: lowered.samplers,
|
||||
static_samplers,
|
||||
root_cbvs: lowered.root_cbvs,
|
||||
root_srvs: lowered.root_srvs,
|
||||
root_uavs: lowered.root_uavs,
|
||||
root_indexed_views: Vec::new(),
|
||||
};
|
||||
if let Ok(mut v) =
|
||||
validator::Validator::load(validator::Validator::default_path().as_deref())
|
||||
{
|
||||
let mut copy = shader.dxil.clone();
|
||||
v.validate(&mut copy).expect("DXIL validation failed");
|
||||
}
|
||||
|
||||
let _one = super::valu_tests::ONE_AT_A_TIME
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner());
|
||||
for (label, warp) in [("hardware", false), ("warp", true)] {
|
||||
let Ok(host) = Host::new(warp) else {
|
||||
continue;
|
||||
};
|
||||
let (root, pso) = host
|
||||
.compute_pipeline(&shader)
|
||||
.unwrap_or_else(|e| panic!("{case_name}/{label}: compute_pipeline failed: {e}"));
|
||||
for branch in [0u32, 1u32] {
|
||||
let mut ud = vec![0u32; shader.user_data_dwords().max(1) as usize];
|
||||
for u in &shader.user_data {
|
||||
ud[u.index as usize] = match (u.root, u.slot) {
|
||||
(RootType::Table, 0) => 0,
|
||||
(RootType::Constants, 1) => branch,
|
||||
other => panic!("unexpected user data {other:?}"),
|
||||
};
|
||||
}
|
||||
let texels = [[1.0f32, 0.0, 0.0, 1.0]];
|
||||
let out = host
|
||||
.dispatch(
|
||||
&root,
|
||||
&pso,
|
||||
&Dispatch {
|
||||
roots: vec![(0, Root::Constants(ud))],
|
||||
views: &[
|
||||
View::Texture(1, 1, &texels),
|
||||
View::Texture(1, 1, &texels),
|
||||
View::RawUav(1),
|
||||
],
|
||||
samplers: &[],
|
||||
groups: [1, 1, 1],
|
||||
},
|
||||
)
|
||||
.unwrap_or_else(|e| panic!("{case_name}/{label}: dispatch failed: {e}"));
|
||||
let got = f32::from_bits(out[0][0]);
|
||||
assert_eq!(got, 1.0, "{case_name}/{label} branch={branch}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user