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
528 changes: 486 additions & 42 deletions crates/victory-gpu/src/descriptor/host.rs

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions crates/victory-gpu/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,24 @@ impl State {
};
}

/// The value of user-data dword `u` of a translated shader: a root
/// argument, or a bound heap's bound (`Lower::heap_bound`): its guard
/// slot (dword 0), a view heap's title slots (dword 1).
fn user_dword(&self, compute: bool, u: &victory_shader::emit::UserData) -> u32 {
let heap = |samplers: bool| {
let h = self.heaps[usize::from(samplers)];
(h != 0)
.then(|| crate::descriptor::heap(h as This).host())
.flatten()
};
match (u.root, u.dword) {
(RootType::ResourceHeap, 0) => heap(false).map_or(0, |h| h.guard_slot()),
(RootType::ResourceHeap, _) => heap(false).map_or(0, |h| h.count()),
(RootType::SamplerHeap, _) => heap(true).map_or(0, |h| h.guard_slot()),
_ => self.user_value(compute, u.slot, u.root, u.dword),
}
}

/// The user-data value of a root argument, as translated shaders read
/// it: a table's byte offset in its heap, a constant's value.
fn user_value(&self, compute: bool, slot: u8, root: RootType, dword: u32) -> u32 {
Expand Down Expand Up @@ -479,7 +497,7 @@ impl State {
.find(|m| m.0 == u.slot && u.root == RootType::Table);
values[u.index as usize] = match fresh {
Some(&(_, offset)) => offset,
None => self.user_value(compute, u.slot, u.root, u.dword),
None => self.user_dword(compute, u),
};
}
// SAFETY: an open list; `values` covers the parameter.
Expand Down Expand Up @@ -567,7 +585,7 @@ impl State {
let t = stage.translated();
let _ = write!(text, " | {}:", stage.shader.hash);
for u in &t.host.user_data {
let v = self.user_value(compute, u.slot, u.root, u.dword);
let v = self.user_dword(compute, u);
let _ = write!(text, " u{}={v:#x}", u.index);
let index = if u.root == RootType::Table { v / 32 } else { v };
if let Some(slot) = views.and_then(|h| h.describe_slot(index)) {
Expand Down
82 changes: 82 additions & 0 deletions crates/victory-gpu/src/pipeline/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,85 @@ fn root_signature(
Ok((device.CreateRootSignature(0, bytes)?, bytes.to_vec()))
}
}

#[cfg(test)]
mod tests {
use super::*;

/// A stage's user data as [`HostPipeline::new`] binds it: root
/// constants, one dword of the root signature each.
fn constants(dwords: u32, visibility: D3D12_SHADER_VISIBILITY) -> D3D12_ROOT_PARAMETER1 {
D3D12_ROOT_PARAMETER1 {
ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS,
Anonymous: D3D12_ROOT_PARAMETER1_0 {
Constants: D3D12_ROOT_CONSTANTS {
ShaderRegister: 0,
RegisterSpace: USER_DATA_SPACE,
Num32BitValues: dwords,
},
},
ShaderVisibility: visibility,
}
}

/// A root CBV as [`HostPipeline::new`] binds one: two dwords.
fn root_cbv(visibility: D3D12_SHADER_VISIBILITY) -> D3D12_ROOT_PARAMETER1 {
D3D12_ROOT_PARAMETER1 {
ParameterType: D3D12_ROOT_PARAMETER_TYPE_CBV,
Anonymous: D3D12_ROOT_PARAMETER1_0 {
Descriptor: D3D12_ROOT_DESCRIPTOR1 {
ShaderRegister: 0,
RegisterSpace: ROOT_CBV_SPACE,
Flags: D3D12_ROOT_DESCRIPTOR_FLAG_DATA_VOLATILE,
},
},
ShaderVisibility: visibility,
}
}

/// The host takes a root signature of up to 64 dwords and refuses one
/// of 65, whatever they are made of (the D3D12 limit); a pipeline whose
/// stages' user data and root views pass it is refused (#747: the
/// heap bounds' user data must not push one over).
#[test]
fn root_signatures_hold_64_dwords() {
let Some(host) = host() else {
return; // no host GPU
};
let (vs, ps) = (
D3D12_SHADER_VISIBILITY_VERTEX,
D3D12_SHADER_VISIBILITY_PIXEL,
);
let cases: [(&str, Vec<D3D12_ROOT_PARAMETER1>, bool); 5] = [
(
"63: 32 + 31",
vec![constants(32, vs), constants(31, ps)],
true,
),
(
"64: 32 + 32",
vec![constants(32, vs), constants(32, ps)],
true,
),
(
"64: 30 + 32 + root CBV",
vec![constants(30, vs), constants(32, ps), root_cbv(ps)],
true,
),
(
"65: 32 + 31 + root CBV",
vec![constants(32, vs), constants(31, ps), root_cbv(ps)],
false,
),
(
"65: 33 + 32",
vec![constants(33, vs), constants(32, ps)],
false,
),
];
for (label, params, takes) in cases {
let made = root_signature(&host.device, &params, &[], false).is_ok();
assert_eq!(made, takes, "{label}");
}
}
}
4 changes: 4 additions & 0 deletions crates/victory-shader/src/emit/diff_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@ fn user_data(s: &HostShader, k: &[u32; 4]) -> Vec<u32> {
(RootType::Table, 3) => 5 * 32,
// The typed views of `raw` (compute) from 9.
(RootType::Table, 6) => 9 * 32,
// Heap bounds: every view these tests bind is in range.
(RootType::ResourceHeap | RootType::SamplerHeap, _) => u32::MAX,
other => panic!("unexpected user data {other:?}"),
};
}
Expand Down Expand Up @@ -1031,6 +1033,7 @@ fn float_semantics_are_the_consoles() {
views: &views,
samplers: &[],
groups: [1, 1, 1],
placed: None,
},
)
.unwrap();
Expand Down Expand Up @@ -1171,6 +1174,7 @@ fn translated_compute_matches_pc() {
views: &views,
samplers: &samplers,
groups: [2, 2, 1],
placed: None,
};
let mut failures = Vec::new();
let only = std::env::var("VICTORY_DIFF_ONLY").ok();
Expand Down
209 changes: 207 additions & 2 deletions crates/victory-shader/src/emit/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,191 @@ pub struct Dispatch<'a> {
pub views: &'a [View<'a>],
pub samplers: &'a [D3D12_SAMPLER_DESC],
pub groups: [u32; 3],
/// Where the views go: `None` in order from slot 0, with the guard
/// slots after them ([`bound`]); else the view heap's size and each
/// view's slot, the guard slots last, every other slot null.
pub placed: Option<(u32, &'a [u32])>,
}

/// The user-data value of a translated shader's heap bound entry (root
/// type `ResourceHeap` or `SamplerHeap`, see `Lower::heap_bound`) for a
/// [`Host::dispatch`] with `views` views in order and `samplers` samplers:
/// the first guard slot after the views, as many title slots as views,
/// the last sampler. `None` for other entries.
#[cfg(test)]
pub fn bound(u: &super::UserData, views: usize, samplers: usize) -> Option<u32> {
use crate::native::RootType;
match u.root {
RootType::ResourceHeap => Some(views as u32),
RootType::SamplerHeap => Some(samplers.saturating_sub(1) as u32),
_ => None,
}
}

/// Makes the slots of view heap `heap` from `first` on the host's guard
/// slots: one null view of each kind of [`super::HEAP_GUARDS`], as
/// `victory-gpu` makes them (`descriptor::host::null_guard`).
#[cfg(test)]
fn null_guards(dev: &ID3D12Device, heap: &ID3D12DescriptorHeap, first: u32) {
let heap_kind = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
// SAFETY: slots inside a live heap; null resources with descriptions,
// a null constant buffer view with none.
unsafe {
let step = dev.GetDescriptorHandleIncrementSize(heap_kind) as usize;
let start = heap.GetCPUDescriptorHandleForHeapStart();
for (i, &guard) in super::HEAP_GUARDS.iter().enumerate() {
let at = D3D12_CPU_DESCRIPTOR_HANDLE {
ptr: start.ptr + (first as usize + i) * step,
};
let (uav, kind) = (guard & 0x1000 != 0, guard & 0xFF);
if kind == 13 {
dev.CreateConstantBufferView(None, at);
} else if uav {
let mut desc = D3D12_UNORDERED_ACCESS_VIEW_DESC {
Format: DXGI_FORMAT_R32_UINT,
..Default::default()
};
desc.ViewDimension = match kind {
1 => D3D12_UAV_DIMENSION_TEXTURE1D,
4 => {
desc.Anonymous.Texture3D.WSize = u32::MAX;
D3D12_UAV_DIMENSION_TEXTURE3D
}
6 => {
desc.Anonymous.Texture1DArray.ArraySize = 1;
D3D12_UAV_DIMENSION_TEXTURE1DARRAY
}
7 => {
desc.Anonymous.Texture2DArray.ArraySize = 1;
D3D12_UAV_DIMENSION_TEXTURE2DARRAY
}
10..=12 => {
desc.Anonymous.Buffer.NumElements = 1;
if kind == 11 {
desc.Format = DXGI_FORMAT_R32_TYPELESS;
desc.Anonymous.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
} else if kind == 12 {
desc.Format = DXGI_FORMAT_UNKNOWN;
desc.Anonymous.Buffer.StructureByteStride = 4;
}
D3D12_UAV_DIMENSION_BUFFER
}
_ => D3D12_UAV_DIMENSION_TEXTURE2D,
};
dev.CreateUnorderedAccessView(None, None, Some(&desc), at);
} else {
let mut desc = D3D12_SHADER_RESOURCE_VIEW_DESC {
Format: DXGI_FORMAT_R8G8B8A8_UNORM,
Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
..Default::default()
};
let a = &mut desc.Anonymous;
desc.ViewDimension = match kind {
1 => {
a.Texture1D.MipLevels = 1;
D3D12_SRV_DIMENSION_TEXTURE1D
}
3 => D3D12_SRV_DIMENSION_TEXTURE2DMS,
4 => {
a.Texture3D.MipLevels = 1;
D3D12_SRV_DIMENSION_TEXTURE3D
}
5 => {
a.TextureCube.MipLevels = 1;
D3D12_SRV_DIMENSION_TEXTURECUBE
}
6 => {
a.Texture1DArray.MipLevels = 1;
a.Texture1DArray.ArraySize = 1;
D3D12_SRV_DIMENSION_TEXTURE1DARRAY
}
7 => {
a.Texture2DArray.MipLevels = 1;
a.Texture2DArray.ArraySize = 1;
D3D12_SRV_DIMENSION_TEXTURE2DARRAY
}
8 => {
a.Texture2DMSArray.ArraySize = 1;
D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY
}
9 => {
a.TextureCubeArray.MipLevels = 1;
a.TextureCubeArray.NumCubes = 1;
D3D12_SRV_DIMENSION_TEXTURECUBEARRAY
}
10..=12 => {
a.Buffer.NumElements = 1;
desc.Format = DXGI_FORMAT_R32_UINT;
if kind == 11 {
desc.Format = DXGI_FORMAT_R32_TYPELESS;
a.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW;
} else if kind == 12 {
desc.Format = DXGI_FORMAT_UNKNOWN;
a.Buffer.StructureByteStride = 4;
}
D3D12_SRV_DIMENSION_BUFFER
}
_ => {
a.Texture2D.MipLevels = 1;
D3D12_SRV_DIMENSION_TEXTURE2D
}
};
dev.CreateShaderResourceView(None, Some(&desc), at);
}
}
}
}

/// Makes `n` slots of view heap `heap` from `first` on null, as the host
/// makes its empty slots (a null 2D texture view): one block made, then
/// copied.
#[cfg(test)]
fn null_views(dev: &ID3D12Device, heap: &ID3D12DescriptorHeap, first: u32, n: u32) {
const BLOCK: u32 = 1 << 16;
if n == 0 {
return;
}
let kind = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
let desc = D3D12_SHADER_RESOURCE_VIEW_DESC {
Format: DXGI_FORMAT_R8G8B8A8_UNORM,
ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D,
Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 {
Texture2D: D3D12_TEX2D_SRV {
MipLevels: 1,
..Default::default()
},
},
};
// SAFETY: slots inside live heaps; the CPU-side block outlives the
// copies.
unsafe {
let step = dev.GetDescriptorHandleIncrementSize(kind) as usize;
let block: ID3D12DescriptorHeap = dev
.CreateDescriptorHeap(&D3D12_DESCRIPTOR_HEAP_DESC {
Type: kind,
NumDescriptors: BLOCK.min(n),
..Default::default()
})
.expect("a CPU descriptor heap");
let start = block.GetCPUDescriptorHandleForHeapStart();
for i in 0..BLOCK.min(n) as usize {
let at = D3D12_CPU_DESCRIPTOR_HANDLE {
ptr: start.ptr + i * step,
};
dev.CreateShaderResourceView(None, Some(&desc), at);
}
let to = heap.GetCPUDescriptorHandleForHeapStart();
let mut done = 0;
while done < n {
let k = BLOCK.min(n - done);
let at = D3D12_CPU_DESCRIPTOR_HANDLE {
ptr: to.ptr + (first + done) as usize * step,
};
dev.CopyDescriptorsSimple(k, at, start, kind);
done += k;
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1071,7 +1256,27 @@ impl Host {
b.Unmap(0, None);
Ok(b)
};
let views = heap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, d.views.len())?;
// In order with a null guard slot after the views (see
// [`bound`]), or placed in a heap whose other slots are null.
let (view_slots, slot_of): (u32, Box<dyn Fn(usize) -> usize>) = match d.placed {
None => (
(d.views.len() + super::HEAP_GUARDS.len()) as u32,
Box::new(|i| i),
),
Some((size, at)) => {
assert_eq!(at.len(), d.views.len(), "a slot for every view");
let guards = super::HEAP_GUARDS.len() as u32;
assert!(
at.iter().all(|&s| s < size - guards),
"slots inside the heap, below its guard slots"
);
(size, Box::new(move |i| at[i] as usize))
}
};
let views = heap(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, view_slots as usize)?;
null_views(dev, &views, 0, view_slots);
// The guard slots, one null view of each kind, end the heap.
null_guards(dev, &views, view_slots - super::HEAP_GUARDS.len() as u32);
let samplers = heap(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, d.samplers.len())?;
let view_size = dev
.GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)
Expand All @@ -1083,7 +1288,7 @@ impl Host {
let mut uavs = Vec::new();
for (i, v) in d.views.iter().enumerate() {
let mut at = views.GetCPUDescriptorHandleForHeapStart();
at.ptr += i * view_size;
at.ptr += slot_of(i) * view_size;
match *v {
View::Texture(..) | View::Cube(..) => {
let (w, h, texels, faces) = match *v {
Expand Down
Loading