Files
6gbvram---16gb-ram/crates/victory-shader/src/emit/shader.rs
T
Claude ffa663571f Shaders: lower waterfall loops over per-lane descriptors in one pass
A descriptor held in VGPRs is used through a waterfall loop: each pass
reads the first active lane's dwords with v_readfirstlane_b32, compares
them with every lane's own (v_cmp_eq), runs the access with the lanes
that match and repeats for the rest. Tracing stopped at the readfirstlane
results ("a descriptor neither loaded nor built").

The tracer now follows the read lanes' dwords to where each lane gets its
own: global_load_dword* from a table or heap at a per-lane address (an
image descriptor's two x4 loads lined up), or a v_cndmask_b32 choice per
lane between such sources (e.g. two s_load descriptors). Other sources
fail naming them (LDS read, buffer load, phi, input, computed VGPRs).
The loop's compares of those reads with their own dwords are recorded.

Lowering gives the access each lane's heap index (its load address over
the descriptor size, or a per-lane select of indices) with
createHandleFromHeap's nonUniformIndex set, lowers the recorded
readfirstlanes as the lane's own dword and their compares as true, so
the loop runs once with all lanes. Wave32 and wave64 are tested.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01AZ9pSB4ZoXYUifuXg8gm66
2026-11-24 02:07:43 +00:00

658 lines
23 KiB
Rust

//! A DXIL vertex, pixel, compute or mesh shader under construction: the module,
//! the `dx.op` declarations it calls, its signatures and bound resources,
//! and the metadata and container that wrap it up (SM 6.6, validator 1.6).
use super::bitcode::{Attr, Md, Module, Ty, V};
use super::container::{self, Binding, Psv, SigElement};
/// Where the shader reads its user data: `b0` in this space.
pub const USER_DATA_SPACE: u32 = 0;
/// Where immediate samplers are bound: `s<n>` in this space.
pub const SAMPLER_SPACE: u32 = 0;
/// Where root CBVs and SRVs are bound: `b<root parameter index>` and
/// `t<root parameter index>` in this space.
pub const ROOT_CBV_SPACE: u32 = 1;
/// `ShaderFlags` bits (metadata) and their `SFI0` feature bits.
pub(crate) mod flags {
pub const RAW_BUFFERS: u64 = 1 << 4;
pub const TYPED_UAV_LOADS: u64 = 1 << 13;
pub const WAVE_OPS: u64 = 1 << 19;
pub const INT64: u64 = 1 << 20;
pub const RESOURCE_HEAP: u64 = 1 << 30;
pub const SAMPLER_HEAP: u64 = 1 << 31;
}
pub(crate) struct Types {
pub void: Ty,
pub i1: Ty,
pub i32: Ty,
pub f32: Ty,
pub handle: Ty,
pub props: Ty,
pub bind: Ty,
pub cbuf_ret: Ty,
pub res_ret: Ty,
}
/// What a mesh shader declares beyond its thread-group size.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Mesh {
pub max_vertices: u32,
pub max_primitives: u32,
/// 1 lines, 2 triangles.
pub topology: u8,
}
pub(crate) struct Shader {
pub m: Module,
pub t: Types,
pub pixel: bool,
pub inputs: Vec<SigElement>,
pub outputs: Vec<SigElement>,
/// A mesh shader's per-primitive outputs.
pub prims: Vec<SigElement>,
/// A mesh shader (`threads` is its thread-group size).
pub mesh: Option<Mesh>,
/// Dwords of user data the shader reads (0: no user-data buffer).
pub user_data: u32,
/// Immediate samplers, bound as `s0..` in [`SAMPLER_SPACE`].
pub samplers: u32,
/// Bit n: immediate sampler n is a comparison sampler.
pub cmp_samplers: u64,
/// Root CBVs read, by root parameter index, with their handles.
pub root_cbvs: Vec<(u8, V)>,
/// Root SRVs (raw buffers) read, likewise.
pub root_srvs: Vec<(u8, V)>,
pub flags: u64,
/// Heap handles made with a non-uniform index.
pub nonuniform_handles: u32,
/// A compute shader with this thread-group size.
pub threads: Option<[u32; 3]>,
/// The wave size a compute shader requires (`WaveSize`).
pub wave_size: Option<u32>,
/// Thread-group shared memory, in dwords.
pub lds_dwords: u32,
lds: Option<V>,
main: V,
pub entry: usize,
}
impl Shader {
pub fn new(pixel: bool) -> Self {
let mut m = Module::default();
let void = m.void();
let i1 = m.int(1);
let i8 = m.int(8);
let i32 = m.int(32);
let f32 = m.float();
let i8p = m.ptr(i8);
let handle = m.named_struct("dx.types.Handle", vec![i8p]);
let props = m.named_struct("dx.types.ResourceProperties", vec![i32, i32]);
let bind = m.named_struct("dx.types.ResBind", vec![i32, i32, i32, i8]);
let cbuf_ret = m.named_struct("dx.types.CBufRet.i32", vec![i32, i32, i32, i32]);
let res_ret = m.named_struct("dx.types.ResRet.f32", vec![f32, f32, f32, f32, i32]);
let main_ty = m.func_ty(void, vec![]);
m.ptr(main_ty);
let main = m.function("main", main_ty, None, true);
let entry = m.new_block();
Shader {
m,
t: Types {
void,
i1,
i32,
f32,
handle,
props,
bind,
cbuf_ret,
res_ret,
},
pixel,
inputs: Vec::new(),
outputs: Vec::new(),
prims: Vec::new(),
mesh: None,
user_data: 0,
samplers: 0,
cmp_samplers: 0,
root_cbvs: Vec::new(),
root_srvs: Vec::new(),
flags: 0,
nonuniform_handles: 0,
threads: None,
wave_size: None,
lds_dwords: 0,
lds: None,
main,
entry,
}
}
/// The thread-group shared memory: `[lds_dwords x i32]` in address
/// space 3.
pub fn lds(&mut self) -> V {
if let Some(g) = self.lds {
return g;
}
let arr = self.m.array(self.lds_dwords, self.t.i32);
let g = self.m.global("lds", arr, 3);
self.lds = Some(g);
g
}
/// Calls `dx.op.<name>` (class and overload) with `opcode` and `args`.
pub fn op(
&mut self,
block: usize,
name: &str,
attr: Attr,
ret: Ty,
opcode: u32,
args: &[V],
) -> V {
let mut params = vec![self.t.i32];
params.extend(args.iter().map(|&a| self.m.type_of(a)));
let fty = self.m.func_ty(ret, params);
self.m.ptr(fty);
let f = self
.m
.function(&format!("dx.op.{name}"), fty, Some(attr), false);
let mut all = vec![self.m.i32(opcode)];
all.extend(args);
self.m.call(block, f, all)
}
/// The handle of the user-data constant buffer, made in the entry block.
pub fn user_data_handle(&mut self) -> V {
let bind = [
self.m.i32(0),
self.m.i32(0),
self.m.i32(USER_DATA_SPACE),
self.m.i8(2), // CBV
];
let bind = self.m.aggregate(self.t.bind, &bind);
let block = self.entry;
let (i, f) = (self.m.i32(0), self.m.bool(false));
let h = self.op(
block,
"createHandleFromBinding",
Attr::ReadNone,
self.t.handle,
217,
&[bind, i, f],
);
// The size is known in `finish`, which fixes this placeholder.
self.annotate(block, h, 13, u32::MAX)
}
/// The handle of the root CBV of root parameter `slot`, made in the
/// entry block.
pub fn root_cbv_handle(&mut self, slot: u8) -> V {
if let Some(&(_, h)) = self.root_cbvs.iter().find(|r| r.0 == slot) {
return h;
}
let r = self.m.i32(u32::from(slot));
let bind = [r, r, self.m.i32(ROOT_CBV_SPACE), self.m.i8(2)];
let bind = self.m.aggregate(self.t.bind, &bind);
let f = self.m.bool(false);
let block = self.entry;
let h = self.op(
block,
"createHandleFromBinding",
Attr::ReadNone,
self.t.handle,
217,
&[bind, r, f],
);
let h = self.annotate(block, h, 13, 65536);
self.root_cbvs.push((slot, h));
h
}
/// The handle of the root SRV (a raw buffer) of root parameter `slot`.
pub fn root_srv_handle(&mut self, slot: u8) -> V {
if let Some(&(_, h)) = self.root_srvs.iter().find(|r| r.0 == slot) {
return h;
}
let r = self.m.i32(u32::from(slot));
let bind = [r, r, self.m.i32(ROOT_CBV_SPACE), self.m.i8(0)];
let bind = self.m.aggregate(self.t.bind, &bind);
let f = self.m.bool(false);
let block = self.entry;
let h = self.op(
block,
"createHandleFromBinding",
Attr::ReadNone,
self.t.handle,
217,
&[bind, r, f],
);
let h = self.annotate(block, h, 11, 0);
self.flags |= flags::RAW_BUFFERS;
self.root_srvs.push((slot, h));
h
}
/// The handle of immediate sampler `n` (a comparison sampler when its
/// bit in `cmp_samplers` is set).
pub fn sampler_handle(&mut self, block: usize, n: u32) -> V {
let bind = [
self.m.i32(n),
self.m.i32(n),
self.m.i32(SAMPLER_SPACE),
self.m.i8(3), // sampler
];
let bind = self.m.aggregate(self.t.bind, &bind);
let (i, f) = (self.m.i32(n), self.m.bool(false));
let h = self.op(
block,
"createHandleFromBinding",
Attr::ReadNone,
self.t.handle,
217,
&[bind, i, f],
);
let cmp = if self.cmp_samplers >> n & 1 != 0 {
0x8000
} else {
0
};
self.annotate(block, h, 14 | cmp, 0)
}
pub fn annotate(&mut self, block: usize, h: V, props0: u32, props1: u32) -> V {
let p = [self.m.i32(props0), self.m.i32(props1)];
let p = self.m.aggregate(self.t.props, &p);
self.op(
block,
"annotateHandle",
Attr::ReadNone,
self.t.handle,
216,
&[h, p],
)
}
/// A descriptor heap handle, annotated with `props`; `nonuniform` when
/// the index may differ between lanes (`NonUniformResourceIndex`).
pub fn heap_handle(
&mut self,
block: usize,
index: V,
sampler: bool,
nonuniform: bool,
props: (u32, u32),
) -> V {
self.flags |= if sampler {
flags::SAMPLER_HEAP
} else {
flags::RESOURCE_HEAP
};
self.nonuniform_handles += u32::from(nonuniform);
let (s, n) = (self.m.bool(sampler), self.m.bool(nonuniform));
let h = self.op(
block,
"createHandleFromHeap",
Attr::ReadNone,
self.t.handle,
218,
&[index, s, n],
);
self.annotate(block, h, props.0, props.1)
}
/// The finished container.
pub fn finish(mut self) -> Vec<u8> {
let ud_size = (self.user_data * 4).next_multiple_of(16);
self.fix_user_data_size(ud_size);
let mut resources = None;
let mut bindings = Vec::new();
let i32c = |s: &mut Self, v: u32| {
let c = s.m.i32(v);
s.m.md_value(c)
};
if self.user_data > 0
|| self.samplers > 0
|| !self.root_cbvs.is_empty()
|| !self.root_srvs.is_empty()
{
let empty = self.m.md_str("");
let mut cbvs = None;
let mut cbv_list = Vec::new();
if self.user_data > 0 {
let arr = self.m.array(self.user_data.next_multiple_of(4), self.t.i32);
let ty = self.m.named_struct("UserData", vec![arr]);
let p = self.m.ptr(ty);
let u = self.m.undef(p);
let ops = vec![
Some(i32c(&mut self, 0)),
Some(self.m.md_value(u)),
Some(empty),
Some(i32c(&mut self, USER_DATA_SPACE)),
Some(i32c(&mut self, 0)),
Some(i32c(&mut self, 1)),
Some(i32c(&mut self, ud_size)),
None,
];
cbv_list.push(Some(self.m.md_node(ops)));
bindings.push(Binding {
kind: 2,
space: USER_DATA_SPACE,
register: 0,
dxil_kind: 13,
});
}
let blob = self.m.array(16384, self.t.i32);
let ty = self.m.named_struct("RootCbv", vec![blob]);
let p = self.m.ptr(ty);
let u = self.m.undef(p);
for (slot, _) in self.root_cbvs.clone() {
let ops = vec![
Some(i32c(&mut self, cbv_list.len() as u32)),
Some(self.m.md_value(u)),
Some(empty),
Some(i32c(&mut self, ROOT_CBV_SPACE)),
Some(i32c(&mut self, u32::from(slot))),
Some(i32c(&mut self, 1)),
Some(i32c(&mut self, 65536)),
None,
];
cbv_list.push(Some(self.m.md_node(ops)));
bindings.push(Binding {
kind: 2,
space: ROOT_CBV_SPACE,
register: u32::from(slot),
dxil_kind: 13,
});
}
if !cbv_list.is_empty() {
cbvs = Some(self.m.md_node(cbv_list));
}
let mut srvs = None;
if !self.root_srvs.is_empty() {
let ty = self
.m
.named_struct("struct.ByteAddressBuffer", vec![self.t.i32]);
let p = self.m.ptr(ty);
let u = self.m.undef(p);
let mut list = Vec::new();
for (n, (slot, _)) in self.root_srvs.clone().into_iter().enumerate() {
let ops = vec![
Some(i32c(&mut self, n as u32)),
Some(self.m.md_value(u)),
Some(empty),
Some(i32c(&mut self, ROOT_CBV_SPACE)),
Some(i32c(&mut self, u32::from(slot))),
Some(i32c(&mut self, 1)),
Some(i32c(&mut self, 11)),
Some(i32c(&mut self, 0)),
None,
];
list.push(Some(self.m.md_node(ops)));
bindings.push(Binding {
kind: 4,
space: ROOT_CBV_SPACE,
register: u32::from(slot),
dxil_kind: 11,
});
}
srvs = Some(self.m.md_node(list));
}
let mut samplers = None;
if self.samplers > 0 {
let ty = self.m.named_struct("struct.SamplerState", vec![self.t.i32]);
let p = self.m.ptr(ty);
let u = self.m.undef(p);
let mut list = Vec::new();
for n in 0..self.samplers {
let kind = (self.cmp_samplers >> n & 1) as u32;
let ops = vec![
Some(i32c(&mut self, n)),
Some(self.m.md_value(u)),
Some(empty),
Some(i32c(&mut self, SAMPLER_SPACE)),
Some(i32c(&mut self, n)),
Some(i32c(&mut self, 1)),
// Sampler kind: 1 comparison.
Some(i32c(&mut self, kind)),
None,
];
list.push(Some(self.m.md_node(ops)));
bindings.push(Binding {
kind: 1,
space: SAMPLER_SPACE,
register: n,
dxil_kind: 14,
});
}
samplers = Some(self.m.md_node(list));
}
resources = Some(self.m.md_node(vec![srvs, None, cbvs, samplers]));
// PSV0 lists them as the module does: CBVs, samplers, SRVs.
bindings.sort_by_key(|b| match b.kind {
2 => 0,
1 => 1,
_ => 2,
});
}
if let Some(r) = resources {
self.m.named_md("dx.resources", vec![r]);
}
let graphics = self.threads.is_none() || self.mesh.is_some();
if graphics {
// shortcut: every output is declared to depend on every input; the
// exact sets need a dependence pass. Drivers read this state.
let scalars = |els: &[SigElement]| {
els.iter()
.filter_map(|e| e.row.map(|r| r * 4 + u32::from(e.col + e.cols)))
.max()
.unwrap_or(0)
};
let (ni, no) = (scalars(&self.inputs), scalars(&self.outputs));
let mut view = vec![ni, no];
// Mesh shaders have no inputs: then the primitive outputs' count.
if self.mesh.is_some() {
view.push(scalars(&self.prims));
}
for _ in 0..ni {
for k in 0..no.div_ceil(32) {
let bits = (no - 32 * k).min(32);
view.push(if bits == 32 {
u32::MAX
} else {
(1 << bits) - 1
});
}
}
let arr = self.m.array(view.len() as u32, self.t.i32);
let elems: Vec<V> = view.iter().map(|&v| self.m.i32(v)).collect();
let arr = self.m.aggregate(arr, &elems);
let arr = self.m.md_value(arr);
let view_node = self.m.md_node(vec![Some(arr)]);
self.m.named_md("dx.viewIdState", vec![view_node]);
}
let sigs = if graphics {
let ins = self.signature_md(self.inputs.clone());
let outs = self.signature_md(self.outputs.clone());
let prims = self.signature_md(self.prims.clone());
Some(self.m.md_node(vec![ins, outs, prims]))
} else {
None
};
// Entry properties: shader flags (tag 0), thread-group size (4),
// wave size (11).
let mut props = Vec::new();
if self.flags != 0 {
props.push(i32c(&mut self, 0));
let i64t = self.m.int(64);
let f = self.m.int_const(i64t, self.flags as i64);
props.push(self.m.md_value(f));
}
if let Some(threads) = self.threads {
let t = threads.map(|n| Some(i32c(&mut self, n)));
let t = self.m.md_node(t.to_vec());
match self.mesh {
// Mesh state (tag 9): threads, maximum vertices and
// primitives, topology, payload bytes.
Some(mesh) => {
props.push(i32c(&mut self, 9));
let state = [
mesh.max_vertices,
mesh.max_primitives,
mesh.topology.into(),
0,
]
.map(|n| Some(i32c(&mut self, n)));
let mut node = vec![Some(t)];
node.extend(state);
props.push(self.m.md_node(node));
}
None => {
props.push(i32c(&mut self, 4));
props.push(t);
}
}
}
if let Some(n) = self.wave_size {
props.push(i32c(&mut self, 11));
let n = Some(i32c(&mut self, n));
props.push(self.m.md_node(vec![n]));
}
let props =
(!props.is_empty()).then(|| self.m.md_node(props.into_iter().map(Some).collect()));
let main = self.m.md_value(self.main);
let name = self.m.md_str("main");
let entry = self
.m
.md_node(vec![Some(main), Some(name), sigs, resources, props]);
let ident = self.m.md_str("victory");
let ident = self.m.md_node(vec![Some(ident)]);
self.m.named_md("llvm.ident", vec![ident]);
let one = i32c(&mut self, 1);
let six = i32c(&mut self, 6);
let version = self.m.md_node(vec![Some(one), Some(six)]);
self.m.named_md("dx.version", vec![version]);
self.m.named_md("dx.valver", vec![version]);
// Program kinds: 0 pixel, 1 vertex, 5 compute, 13 mesh.
let (name, kind) = match (self.threads, self.pixel) {
_ if self.mesh.is_some() => ("ms", 13u32),
(Some(_), _) => ("cs", 5u32),
(None, true) => ("ps", 0),
(None, false) => ("vs", 1),
};
let name = self.m.md_str(name);
let sm = self.m.md_node(vec![Some(name), Some(six), Some(six)]);
self.m.named_md("dx.shaderModel", vec![sm]);
self.m.named_md("dx.entryPoints", vec![entry]);
let bitcode = self.m.write();
let mut dxil = Vec::with_capacity(24 + bitcode.len());
dxil.extend((kind << 16 | 0x66).to_le_bytes());
dxil.extend((((24 + bitcode.len()) / 4) as u32).to_le_bytes());
dxil.extend(b"DXIL");
dxil.extend(0x106u32.to_le_bytes());
dxil.extend(16u32.to_le_bytes());
dxil.extend((bitcode.len() as u32).to_le_bytes());
dxil.extend(&bitcode);
let mut features = 0u64;
if self.flags & flags::TYPED_UAV_LOADS != 0 {
features |= 0x800;
}
if self.flags & flags::WAVE_OPS != 0 {
features |= 0x4000;
}
if self.flags & flags::INT64 != 0 {
features |= 0x8000;
}
if self.flags & flags::RESOURCE_HEAP != 0 {
features |= 0x200_0000;
}
if self.flags & flags::SAMPLER_HEAP != 0 {
features |= 0x400_0000;
}
let sfi0 = features.to_le_bytes();
let isg1 = container::signature(&self.inputs, true);
let osg1 = container::signature(&self.outputs, false);
let flag = if self.pixel {
self.outputs.iter().any(|o| (17..=19).contains(&o.kind))
} else {
self.outputs.iter().any(|o| o.kind == 3)
};
let psv0 = container::psv(&Psv {
stage: kind as u8,
flag: flag && self.threads.is_none(),
inputs: &self.inputs,
outputs: &self.outputs,
prims: &self.prims,
mesh: self.mesh.map(|m| (m, self.lds_dwords * 4)),
bindings: &bindings,
threads: self.threads.unwrap_or_default(),
wave_size: self.wave_size,
});
let hash = [0u8; 20];
let psg1 = container::signature(&self.prims, false);
let mut parts: Vec<(&[u8; 4], &[u8])> =
vec![(b"SFI0", &sfi0), (b"ISG1", &isg1), (b"OSG1", &osg1)];
if self.mesh.is_some() {
parts.push((b"PSG1", &psg1));
}
parts.extend([(b"PSV0", &psv0[..]), (b"HASH", &hash), (b"DXIL", &dxil)]);
container::build(&parts)
}
/// The user-data buffer's annotation carries its size, known only now.
fn fix_user_data_size(&mut self, size: u32) {
let old = [self.m.i32(13), self.m.i32(u32::MAX)];
let old = self.m.aggregate(self.t.props, &old);
let new = [self.m.i32(13), self.m.i32(size)];
let new = self.m.aggregate(self.t.props, &new);
self.m.replace_const(old, new);
}
fn signature_md(&mut self, elements: Vec<SigElement>) -> Option<Md> {
if elements.is_empty() {
return None;
}
let mut list = Vec::new();
for (id, e) in elements.iter().enumerate() {
let c32 = |s: &mut Self, v: u32| {
let c = s.m.i32(v);
Some(s.m.md_value(c))
};
let c8 = |s: &mut Self, v: u8| {
let c = s.m.i8(v);
Some(s.m.md_value(c))
};
let idx = c32(self, e.index);
let indices = Some(self.m.md_node(vec![idx]));
let usage = if e.usage != 0 {
let (tag, mask) = (c32(self, 3), c32(self, u32::from(e.usage)));
Some(self.m.md_node(vec![tag, mask]))
} else {
None
};
let ops = vec![
c32(self, id as u32),
Some(self.m.md_str(&e.name)),
c8(self, e.comp),
c8(self, e.kind),
indices,
c8(self, e.interp),
c32(self, 1),
c8(self, e.cols),
c32(self, e.row.unwrap_or(u32::MAX)),
c8(self, if e.row.is_some() { e.col } else { u8::MAX }),
usage,
];
list.push(Some(self.m.md_node(ops)));
}
Some(self.m.md_node(list))
}
}