GPU: the world stays up; indirect execution behind a switch
The host device was removed seconds into the world because a shader read a descriptor of the wrong kind: empty slots held a null 2D texture view where the shader expected a buffer, which gave a garbage heap index and a read far past the view heap. Null descriptors now have the kind the shader expects (raw buffer, constant buffer, or a texture of the title's own dimension). The world then ran to a 240 s time limit with no removal. Also: - Host lists hold the textures, pipelines, root signatures and heaps they name, and heap slots hold the resource they view (the title releases objects before submitting lists that use them). - The device-removed report places the fault against the host descriptor heaps and names our own uploads; VICTORY_D3D12_GBV turns on GPU-based validation, VICTORY_DUMP_TARGETS works. - Indirect execution (draw, indexed draw, dispatch, mesh dispatch): the world draws most of its geometry this way. Arguments are clamped and charged to a per-list budget by a small compute pass first, so garbage cannot hang the GPU. Off by default (VICTORY_INDIRECT=1): with it on the title's own GPU watchdog ends the run about 30 s into the world. - Shader-written ranges are copied back once per submission and only where they differ; refreshes beside them copy only stale pages. The frame-rate line counts copy-backs and dropped draws. Not working yet: the world's picture is black (indirect draws off, and hundreds of draws per frame have no translated shader). Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
1 parent
1922261412
commit
d50f2368cb
14 files changed
+973
-151
No files matched your search
@@ -0,0 +1,209 @@
|
||||
//! Indirect execution's arguments, bounded before the GPU reads them. The
|
||||
//! title's own compute passes write most of them; a pass we drop or
|
||||
//! translate wrongly leaves whatever the memory held, and a draw of four
|
||||
//! billion vertices or a dispatch past the host's limits runs until the
|
||||
//! host resets the display driver. So every record is clamped on the GPU,
|
||||
//! just before its execution, by a compute shader of ours (HLSL compiled by
|
||||
//! the system's compiler; a 5.1 shader with its root signature inside).
|
||||
//!
|
||||
//! Each list also has a budget (2^28 vertex instances or threads, mesh
|
||||
//! groups counted as 128): records past it run empty, so a list of
|
||||
//! thousands of garbage records cannot run for seconds either.
|
||||
//!
|
||||
//! shortcut: the bounds (2^24 vertices, 2^26 vertex instances, 65,535
|
||||
//! groups per dimension, one-dimensional mesh dispatches, the budget) cut
|
||||
//! legitimate work beyond them; raise them if the title needs more.
|
||||
|
||||
use windows::Win32::Graphics::Direct3D::Fxc::D3DCompile;
|
||||
use windows::Win32::Graphics::Direct3D::ID3DBlob;
|
||||
use windows::Win32::Graphics::Direct3D12::*;
|
||||
use windows::core::s;
|
||||
|
||||
use crate::host::{HostList, host, transition};
|
||||
|
||||
const SOURCE: &str = r#"
|
||||
#define RS "RootConstants(num32BitConstants=4, b0), UAV(u0), UAV(u1)"
|
||||
cbuffer Record : register(b0) { uint kind; uint stride; uint records; uint unused; };
|
||||
RWByteAddressBuffer args : register(u0);
|
||||
// The list's work so far (vertex instances, threads): past the budget,
|
||||
// records run empty.
|
||||
RWByteAddressBuffer budget : register(u1);
|
||||
static const uint BUDGET = 1u << 28;
|
||||
[RootSignature(RS)]
|
||||
[numthreads(64, 1, 1)]
|
||||
void main(uint3 id : SV_DispatchThreadID) {
|
||||
if (id.x >= records) return;
|
||||
uint at = id.x * stride;
|
||||
uint cost;
|
||||
if (kind <= 1) {
|
||||
// Draws: vertex or index count, then instances.
|
||||
uint count = min(args.Load(at), 1u << 24);
|
||||
uint instances = min(args.Load(at + 4), (1u << 26) / max(count, 1u));
|
||||
cost = count * instances;
|
||||
args.Store2(at, uint2(count, instances));
|
||||
} else if (kind == 2) {
|
||||
uint3 g = min(args.Load3(at), uint3(65535, 65535, 65535));
|
||||
cost = min(g.x * g.y * g.z, 1u << 20) * 64;
|
||||
args.Store3(at, g);
|
||||
} else {
|
||||
// Mesh dispatches: the console's are one-dimensional.
|
||||
uint3 g = args.Load3(at);
|
||||
g = uint3(min(g.x, 65535u), min(g.y, 1u), min(g.z, 1u));
|
||||
cost = g.x * g.y * g.z * 128;
|
||||
args.Store3(at, g);
|
||||
}
|
||||
uint before;
|
||||
budget.InterlockedAdd(0, cost, before);
|
||||
if (before >= BUDGET || before + cost > BUDGET) {
|
||||
// Draws keep their layout with no vertices; groups become zero.
|
||||
args.Store(at, 0);
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
struct Clamp {
|
||||
root: ID3D12RootSignature,
|
||||
pso: ID3D12PipelineState,
|
||||
}
|
||||
|
||||
// SAFETY: D3D12 objects are free-threaded.
|
||||
unsafe impl Send for Clamp {}
|
||||
// SAFETY: as above.
|
||||
unsafe impl Sync for Clamp {}
|
||||
|
||||
fn clamp() -> Option<&'static Clamp> {
|
||||
static CLAMP: std::sync::OnceLock<Option<Clamp>> = std::sync::OnceLock::new();
|
||||
CLAMP
|
||||
.get_or_init(|| match make() {
|
||||
Ok(c) => Some(c),
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %e, "no argument clamp: indirect execution dropped");
|
||||
None
|
||||
}
|
||||
})
|
||||
.as_ref()
|
||||
}
|
||||
|
||||
fn make() -> windows::core::Result<Clamp> {
|
||||
let device = &host().ok_or_else(windows::core::Error::empty)?.device;
|
||||
let mut code: Option<ID3DBlob> = None;
|
||||
let mut errors: Option<ID3DBlob> = None;
|
||||
// SAFETY: the source and names live through the call; out pointers ours.
|
||||
let compiled = unsafe {
|
||||
D3DCompile(
|
||||
SOURCE.as_ptr().cast(),
|
||||
SOURCE.len(),
|
||||
s!("clamp"),
|
||||
None,
|
||||
None,
|
||||
s!("main"),
|
||||
s!("cs_5_1"),
|
||||
0,
|
||||
0,
|
||||
&mut code,
|
||||
Some(&mut errors),
|
||||
)
|
||||
};
|
||||
if let Err(e) = compiled {
|
||||
if let Some(errors) = errors {
|
||||
// SAFETY: the blob's own bytes.
|
||||
let text = unsafe {
|
||||
std::slice::from_raw_parts(errors.GetBufferPointer().cast::<u8>(), errors.GetBufferSize())
|
||||
};
|
||||
tracing::warn!("argument clamp: {}", String::from_utf8_lossy(text));
|
||||
}
|
||||
return Err(e);
|
||||
}
|
||||
let code = code.ok_or_else(windows::core::Error::empty)?;
|
||||
// SAFETY: the blob's own bytes; the root signature is inside the shader.
|
||||
unsafe {
|
||||
let bytes = std::slice::from_raw_parts(code.GetBufferPointer().cast::<u8>(), code.GetBufferSize());
|
||||
let root: ID3D12RootSignature = device.CreateRootSignature(0, bytes)?;
|
||||
let pso = device.CreateComputePipelineState(&D3D12_COMPUTE_PIPELINE_STATE_DESC {
|
||||
pRootSignature: std::mem::transmute_copy(&root),
|
||||
CS: D3D12_SHADER_BYTECODE {
|
||||
pShaderBytecode: bytes.as_ptr().cast(),
|
||||
BytecodeLength: bytes.len(),
|
||||
},
|
||||
..Default::default()
|
||||
})?;
|
||||
Ok(Clamp { root, pso })
|
||||
}
|
||||
}
|
||||
|
||||
/// Records, in `list`, the clamp of `records` records of `kind` (PC
|
||||
/// argument numbering) `stride` bytes apart at host address `gpu` in
|
||||
/// `buffer`, and leaves the buffer ready to be read as arguments. Replaces
|
||||
/// the list's compute root signature and pipeline (callers bind theirs
|
||||
/// after). False if the clamp is not available.
|
||||
pub(crate) fn bound(
|
||||
list: &HostList,
|
||||
buffer: &ID3D12Resource,
|
||||
gpu: u64,
|
||||
(kind, stride, records): (u32, u32, u32),
|
||||
) -> bool {
|
||||
let (Some(c), Some(budget)) = (clamp(), list.budget()) else {
|
||||
return false;
|
||||
};
|
||||
if !gpu.is_multiple_of(4) || !stride.is_multiple_of(4) {
|
||||
return false;
|
||||
}
|
||||
let l = &list.list;
|
||||
let constants = [kind, stride, records, 0];
|
||||
// SAFETY: an open list; our root signature, pipeline and a live buffer
|
||||
// the caller's list holds.
|
||||
unsafe {
|
||||
// Earlier shader writes (the title's argument passes) first.
|
||||
l.ResourceBarrier(&[uav_barrier()]);
|
||||
l.SetComputeRootSignature(&c.root);
|
||||
l.SetPipelineState(&c.pso);
|
||||
l.SetComputeRoot32BitConstants(0, 4, constants.as_ptr().cast(), 0);
|
||||
l.SetComputeRootUnorderedAccessView(1, gpu);
|
||||
l.SetComputeRootUnorderedAccessView(2, budget);
|
||||
l.Dispatch(records.div_ceil(64), 1, 1);
|
||||
l.ResourceBarrier(&[
|
||||
uav_barrier(),
|
||||
transition(
|
||||
buffer,
|
||||
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
|
||||
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||
),
|
||||
]);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// After the execution: the buffer back to unordered access, where the
|
||||
/// title's passes keep using it.
|
||||
pub(crate) fn done(list: &HostList, buffer: &ID3D12Resource) {
|
||||
// SAFETY: an open list and a live buffer.
|
||||
unsafe {
|
||||
list.list.ResourceBarrier(&[transition(
|
||||
buffer,
|
||||
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
|
||||
)])
|
||||
};
|
||||
}
|
||||
|
||||
fn uav_barrier() -> D3D12_RESOURCE_BARRIER {
|
||||
D3D12_RESOURCE_BARRIER {
|
||||
Type: D3D12_RESOURCE_BARRIER_TYPE_UAV,
|
||||
Flags: D3D12_RESOURCE_BARRIER_FLAG_NONE,
|
||||
Anonymous: D3D12_RESOURCE_BARRIER_0 {
|
||||
UAV: std::mem::ManuallyDrop::new(D3D12_RESOURCE_UAV_BARRIER { pResource: std::mem::ManuallyDrop::new(None) }),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
/// The clamp compiles with the system's compiler and the host takes it.
|
||||
#[test]
|
||||
fn argument_clamp_builds() {
|
||||
if super::host().is_none() {
|
||||
return; // no host GPU here
|
||||
}
|
||||
assert!(super::clamp().is_some());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user