Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions crates/victory-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub(crate) struct HostHeap {
/// (`victory_shader::emit::SLOT_STRIDES`): an upload buffer of
/// `SLOT_STRIDES_COUNT` dwords, its GPU address and its mapping.
strides: Option<(ID3D12Resource, u64, usize)>,
/// View heaps: the title address of the buffer view at each title slot,
/// two dwords per slot, for shaders that read a descriptor's address out
/// of a table at an index they compute
/// (`victory_shader::emit::SLOT_VIEWS`).
views: Option<(ID3D12Resource, u64, usize)>,
/// Title slots holding buffer views. Such a view gets its host slot
/// (and its memory a host copy) only when something reads it: a draw
/// through a table, or [`HostHeap::buffer_slots`] for shaders that pick
Expand Down Expand Up @@ -687,6 +692,19 @@ impl HostHeap {
Some((buffer, gpu, data as usize))
})
.flatten();
let views = (!sampler)
.then(|| {
let bytes = u64::from(victory_shader::emit::SLOT_VIEWS_COUNT) * 16;
let (buffer, data) = host.upload_buffer(bytes)?;
// SAFETY: the buffer's whole mapping; a live buffer.
let gpu = unsafe {
std::ptr::write_bytes(data, 0, bytes as usize);
buffer.GetGPUVirtualAddress()
};
crate::host::name(&buffer, || "view addresses by view heap slot".to_owned());
Some((buffer, gpu, data as usize))
})
.flatten();
let h = HostHeap {
heap,
cpu,
Expand All @@ -700,6 +718,7 @@ impl HostHeap {
free: std::sync::atomic::AtomicU32::new(DYNAMIC - 1),
too_long: std::sync::atomic::AtomicU32::new(u32::MAX),
strides,
views,
buffers: Mutex::default(),
changed: Mutex::default(),
textures: Mutex::default(),
Expand Down Expand Up @@ -853,6 +872,16 @@ impl HostHeap {
self.strides.as_ref().map(|s| &s.0)
}

/// The GPU address of the table of view addresses by title slot.
pub(crate) fn views_gpu(&self) -> Option<u64> {
self.views.as_ref().map(|s| s.1)
}

/// The view-address table's buffer, for a list to hold.
pub(crate) fn views_buffer(&self) -> Option<&ID3D12Resource> {
self.views.as_ref().map(|s| &s.0)
}

/// Up to `max` title slots whose texture view is due at `frame`, each
/// with its write number, in the order to serve them
/// ([`TextureQueue::due`]), and the queue's counts
Expand Down Expand Up @@ -1262,6 +1291,21 @@ impl HostHeap {
// dword, either value is one the slot had).
unsafe { (*data as *mut u32).add(index as usize).write_volatile(stride.unwrap_or(0)) };
}
let range = view.and_then(buffer_range);
if let Some((_, _, data)) = &self.views
&& index < victory_shader::emit::SLOT_VIEWS_COUNT
{
// SAFETY: inside the mapping; a whole slot, written at once:
// the address, the byte length and the element stride.
unsafe {
let at = (*data as *mut u32).add(index as usize * 4);
let (address, len) = range.unwrap_or((0, 0));
at.write_volatile(address as u32);
at.add(1).write_volatile((address >> 32) as u32);
at.add(2).write_volatile(len as u32);
at.add(3).write_volatile(stride.unwrap_or(0));
};
}
let mut buffers = self.buffers.lock().unwrap_or_else(|e| e.into_inner());
let was = if stride.is_some() {
buffers.insert(index);
Expand Down
37 changes: 37 additions & 0 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,23 @@ impl State {
}
continue;
}
if srv && slot == victory_shader::emit::SLOT_VIEWS {
// Ours: the title addresses of the heap's buffer
// views, for a descriptor read as data.
match self.heap_views(list) {
// SAFETY: an open list; the table lives as long
// as the heap the list holds.
Some(gpu) => unsafe {
if compute {
l.SetComputeRootShaderResourceView(param, gpu);
} else {
l.SetGraphicsRootShaderResourceView(param, gpu);
}
},
None => once!("a shader reads view addresses of a heap that is not set"),
}
continue;
}
let tabled = srv && crate::pipeline::host::bounded_root_srvs();
let Some(&Root::View(address)) = self.roots_ref(compute).get(usize::from(slot))
else {
Expand Down Expand Up @@ -1467,6 +1484,26 @@ impl State {
Some(gpu)
}

/// [`State::heap_buffers`] for a shader that reads a descriptor's
/// address as data: the GPU address of the host's table of the buffer
/// views' title addresses by heap slot. The pass that readies the
/// buffer views also fills the table.
fn heap_views(&self, list: &HostList) -> Option<u64> {
let heap = self
.heaps
.iter()
.filter(|&&h| h != 0)
.map(|&h| crate::descriptor::heap(h as This))
.find(|h| !h.samplers())?;
let host = heap.host()?;
let gpu = host.views_gpu()?;
self.heap_buffers(list)?;
if let Some(buffer) = host.views_buffer() {
list.keep(buffer);
}
Some(gpu)
}

/// The texture side of [`State::heap_buffers`]: textures the title fills
/// itself (streamed pages, layout-library copies) whose views sit in the
/// view heap are created (placed textures have no host texture until
Expand Down
1 change: 1 addition & 0 deletions crates/victory-gpu/src/pipeline/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ impl HostPipeline {
slot == victory_shader::emit::SLOT_FAULTS
} else {
slot == victory_shader::emit::SLOT_STRIDES
|| slot == victory_shader::emit::SLOT_VIEWS
|| slot == victory_shader::emit::SLOT_MEMORY
};
if (uav || kind == D3D12_ROOT_PARAMETER_TYPE_SRV) && !ours && bounded_root_srvs() {
Expand Down
172 changes: 171 additions & 1 deletion crates/victory-shader/src/emit/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2663,6 +2663,60 @@ impl<'a> Lower<'a> {
Some((lo, hi))
}

/// [`view_address`] for a table entry chosen at run time (a computed
/// index into a descriptor table or the resource heap): the host's table
/// of the buffer views' title addresses by heap slot
/// ([`shader::SLOT_VIEWS`]).
fn view_address_indexed(&mut self, site: Site, first: u8) -> Result<Option<(V, V, V, V)>, Why> {
let a = *self
.scalar
.get(&site)
.ok_or("an untracked descriptor load")?;
let root = self
.x
.native
.layout
.get(a.param)
.map(|p| p.root)
.ok_or("a descriptor outside the tables")?;
if !matches!(root, RootType::Table | RootType::ResourceHeap) || a.dynamic.is_none() {
return Ok(None);
}
let c = self.cur;
let slot = self.heap_index(site, first, false)?;
let last = self.s.m.i32(super::shader::SLOT_VIEWS_COUNT - 1);
let i32t = self.s.t.i32;
let slot = self.s.op(c, "binary.i32", Attr::ReadNone, i32t, 40, &[slot, last]);
self.s.m.tag(slot, Tag::Clamp);
let three = self.s.m.i32(3);
let byte = self.s.m.bin(c, Bin::Shl, slot, three);
let h = self.s.root_srv_handle(super::shader::SLOT_VIEWS);
let iu = self.s.m.undef(self.s.t.i32);
let mask = self.s.m.i8(0xF);
let align = self.s.m.i32(4);
let r = self.s.op(
c,
"rawBufferLoad.f32",
Attr::ReadOnly,
self.s.t.res_ret,
139,
&[h, byte, iu, mask, align],
);
let lo = self.s.m.extract(c, r, 0);
let hi = self.s.m.extract(c, r, 1);
let len = self.s.m.extract(c, r, 2);
let stride = self.s.m.extract(c, r, 3);
let (lo, hi, len, stride) = (
self.as_int(lo),
self.as_int(hi),
self.as_int(len),
self.as_int(stride),
);
let off = a.offset as i32;
let (lo, hi) = self.address_plus(lo, hi, off);
Ok(Some((lo, hi, len, stride)))
}

/// `lo`, `hi` plus the instruction's byte offset `off`, with the carry.
fn address_plus(&mut self, lo: V, hi: V, off: i32) -> (V, V) {
if off == 0 {
Expand Down Expand Up @@ -5618,7 +5672,9 @@ impl<'a> Lower<'a> {
}
// A buffer view the code only takes the address of, to walk
// the memory behind it (a ray-tracing structure): the address
// from the host, the other dwords have no value.
// from the host, the other dwords have no value. A table entry
// chosen at run time asks the host's table of view addresses;
// one load may carry two descriptors (8 dwords).
if count <= 4
&& let Some((lo, hi)) = self.view_address(site)
{
Expand All @@ -5637,6 +5693,31 @@ impl<'a> Lower<'a> {
}
return Ok(());
}
if count <= 8 && count % 4 == 0 {
let mut wrote = false;
for group in 0..count / 4 {
let first = (4 * group) as u8;
let Some((lo, hi, len, stride)) =
self.view_address_indexed(site, first).ok().flatten()
else {
continue;
};
wrote = true;
for k in 4 * group..4 * group + 4 {
let reg = Reg::S(code + k as u8);
let v = match k % 4 {
0 => lo,
1 => hi,
2 => len,
_ => stride,
};
self.write(inst, reg, v, None);
}
}
if wrote {
return Ok(());
}
}
return Err("a descriptor read only as data");
};
let src = Source::Loaded { load: site, first };
Expand Down Expand Up @@ -7040,6 +7121,44 @@ impl<'a> Lower<'a> {
let r = self.s.m.cast(c, op, a, self.s.t.i32);
int(self, r)
}
"v_permlane16_b32" | "v_permlanex16_b32" => {
// Each lane reads src0 of a lane of its own 32-lane half,
// picked by nibble (lane & 15) of {src2, src1}: within its
// own row of 16 (`v_permlane16`) or the other row of its
// half (`v_permlanex16`). The pick stays inside a half, so a
// console wave of 64 run as two host waves of 32 needs no
// exchange.
// shortcut: a source lane with EXEC clear is read as the host
// gives it (the console keeps vdst unless op_sel's fetch
// inactive bit is set).
self.cross_lane(false)?;
let x = s(self, 0)?;
let (s1, s2) = (s(self, 1)?, s(self, 2)?);
let i32t = self.s.t.i32;
let lane = self.s.op(c, "waveGetLaneIndex", Attr::ReadOnly, i32t, 111, &[]);
let [k15, k8, k7, k2, k16, not31, not15] =
[15, 8, 7, 2, 16, !31, !15].map(|x| self.s.m.i32(x));
let low = self.s.m.bin(c, Bin::And, lane, k15);
// nibble `low` of the 64-bit selector: src1 holds lanes 0-7
let hi_word = self.s.m.cmp(c, pred::IUGE, low, k8);
let word = self.s.m.select(c, hi_word, s2, s1);
let n = self.s.m.bin(c, Bin::And, low, k7);
let sh = self.s.m.bin(c, Bin::Shl, n, k2);
let pick = self.s.m.bin(c, Bin::LShr, word, sh);
let pick = self.s.m.bin(c, Bin::And, pick, k15);
let row = if name == "v_permlanex16_b32" {
// the other row of this half
let half = self.s.m.bin(c, Bin::And, lane, not31);
let other = self.s.m.bin(c, Bin::And, lane, k16);
let other = self.s.m.bin(c, Bin::Xor, other, k16);
self.s.m.bin(c, Bin::Or, half, other)
} else {
self.s.m.bin(c, Bin::And, lane, not15)
};
let from = self.s.m.bin(c, Bin::Or, row, pick);
let r = self.read_lane(x, from);
int(self, r)
}
"v_cvt_f32_i32" | "v_cvt_f32_u32" | "v_cvt_f32_ubyte0" | "v_cvt_f32_ubyte1"
| "v_cvt_f32_ubyte2" | "v_cvt_f32_ubyte3" => {
let mut a = s(self, 0)?;
Expand Down Expand Up @@ -7169,6 +7288,57 @@ impl<'a> Lower<'a> {
.op(c, "legacyF16ToF32", Attr::ReadNone, self.s.t.f32, 131, &[a]);
float(self, r)
}
"v_cvt_f32_f64" if v.opsel == 0 => {
// IEEE-754 f64 (sign:1, exp:11 bias 1023, frac:52) -> f32
// in integer ops so no DXIL f64/doubles feature flag is needed.
let x = self.src64(inst, 0)?;
let (lo, mut hi) = self.split64(x);
if v.abs & 1 != 0 {
let m = self.s.m.i32(0x7FFF_FFFF);
hi = self.s.m.bin(c, Bin::And, hi, m);
}
if v.neg & 1 != 0 {
let s = self.s.m.i32(0x8000_0000);
hi = self.s.m.bin(c, Bin::Xor, hi, s);
}
let sign_mask = self.s.m.i32(0x8000_0000);
let sign = self.s.m.bin(c, Bin::And, hi, sign_mask);
let k20 = self.s.m.i32(20);
let exp_mask = self.s.m.i32(0x7FF);
let exp_raw = self.s.m.bin(c, Bin::LShr, hi, k20);
let exp64 = self.s.m.bin(c, Bin::And, exp_raw, exp_mask);
let frac_hi_mask = self.s.m.i32(0x000F_FFFF);
let frac_hi = self.s.m.bin(c, Bin::And, hi, frac_hi_mask);
let k3 = self.s.m.i32(3);
let k29 = self.s.m.i32(29);
let mant_hi = self.s.m.bin(c, Bin::Shl, frac_hi, k3);
let mant_lo = self.s.m.bin(c, Bin::LShr, lo, k29);
let mant = self.s.m.bin(c, Bin::Or, mant_hi, mant_lo);
let bias_diff = self.s.m.i32(896);
let exp32 = self.s.m.bin(c, Bin::Sub, exp64, bias_diff);
let k23 = self.s.m.i32(23);
let exp32_sh = self.s.m.bin(c, Bin::Shl, exp32, k23);
let normal = self.s.m.bin(c, Bin::Or, sign, exp32_sh);
let normal = self.s.m.bin(c, Bin::Or, normal, mant);
let k897 = self.s.m.i32(897);
let under = self.s.m.cmp(c, pred::IULT, exp64, k897);
let k1151 = self.s.m.i32(1151);
let over = self.s.m.cmp(c, pred::IUGE, exp64, k1151);
let inf_bits = self.s.m.i32(0x7F80_0000);
let inf = self.s.m.bin(c, Bin::Or, sign, inf_bits);
let k2047 = self.s.m.i32(2047);
let is_max = self.s.m.cmp(c, pred::EQ, exp64, k2047);
let frac_or = self.s.m.bin(c, Bin::Or, frac_hi, lo);
let z = self.s.m.i32(0);
let has_frac = self.s.m.cmp(c, pred::NE, frac_or, z);
let is_nan = self.s.m.bin(c, Bin::And, is_max, has_frac);
let qnan = self.s.m.i32(0x7FC0_0000);
let over_val = self.s.m.select(c, is_nan, qnan, inf);
let r = self.s.m.select(c, under, sign, normal);
let r = self.s.m.select(c, over, over_val, r);
let f = self.as_float(r);
float(self, f)
}
"v_cndmask_b32" => {
let mask = match v.src.get(2) {
Some(&Operand::Scalar { code, .. }) => inst.read(Reg::S(code)),
Expand Down
3 changes: 2 additions & 1 deletion crates/victory-shader/src/emit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ pub use lower::{host_max_wave, last_waves, set_host_max_wave};
pub use rect::rect_list_gs;
pub use shader::{
MEMORY_FAULTS, MEMORY_PAGES, ROOT_CBV_SPACE, SAMPLER_SPACE, SLOT_FAULTS, SLOT_MEMORY, SLOT_STRIDES,
SLOT_STRIDES_COUNT, USER_DATA_SPACE, USER_GROUP_BASE, USER_MEMORY_BASE, USER_VIEW_ADDRESS, USER_VIEW_BIAS,
SLOT_STRIDES_COUNT, SLOT_VIEWS, SLOT_VIEWS_COUNT, USER_DATA_SPACE, USER_GROUP_BASE, USER_MEMORY_BASE,
USER_VIEW_ADDRESS, USER_VIEW_BIAS,
set_view_heap_limit, view_heap_limit,
};

Expand Down
11 changes: 11 additions & 0 deletions crates/victory-shader/src/emit/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ pub const SLOT_STRIDES: u8 = 255;
/// Dwords of the [`SLOT_STRIDES`] buffer: heap indices are clamped to it.
pub const SLOT_STRIDES_COUNT: u32 = 1 << 21;

/// Title addresses of the buffer views of the view heap by slot, for
/// console code that reads a descriptor's address out of a table at an
/// index it computes (ray-tracing structures and the programs that walk
/// them) and finds no root view to ask the host about. Two dwords per
/// slot: the title address of the view's first byte. Slots without a
/// buffer view read zero. Like [`SLOT_STRIDES`], the host readies the
/// buffer views of the whole heap when a shader has this root SRV.
pub const SLOT_VIEWS: u8 = 252;
/// Slots of the [`SLOT_VIEWS`] buffer (heap indices are clamped to it).
pub const SLOT_VIEWS_COUNT: u32 = SLOT_STRIDES_COUNT;

/// Title memory by address (`resource::MEMORY`): console code reads and
/// writes through raw 40-bit addresses it takes from root constants,
/// descriptors and other memory (the ray-tracing structure builder and the
Expand Down
Loading