Skip to content
Merged
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
133 changes: 121 additions & 12 deletions crates/xcl/src/hle/d3d12x/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,20 +1137,29 @@ struct DrawSnap {
enum RunItem {
Draw(DeferredDraw),
Bind { pso: usize, _held: Option<ID3D12PipelineState> },
StencilRef(u32),
Topology(u32),
Scissors(Vec<[i32; 4]>),
}

/// A run item as `batch_commands` takes it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
enum Step {
Draw(DrawSnap),
Bind(usize),
StencilRef(u32),
Topology(u32),
Scissors(Vec<[i32; 4]>),
}

impl RunItem {
fn step(&self) -> Step {
match self {
RunItem::Draw(d) => Step::Draw(d.snap),
RunItem::Bind { pso, .. } => Step::Bind(*pso),
RunItem::StencilRef(r) => Step::StencilRef(*r),
RunItem::Topology(t) => Step::Topology(*t),
RunItem::Scissors(r) => Step::Scissors(r.clone()),
}
}
}
Expand Down Expand Up @@ -1224,7 +1233,7 @@ fn batch_region_bytes(need: u64) -> u64 {
/// the console's own methods – records the run first.
fn keeps_two_pass_batch(slot: usize, newer_layout: bool) -> bool {
let (ei, dm) = if newer_layout { (61, 129) } else { (59, 127) };
matches!(slot, 0..=8 | 25 | 33..=46) || slot == ei || slot == dm
matches!(slot, 0..=8 | 20 | 22 | 24 | 25 | 33..=46) || slot == ei || slot == dm || (newer_layout && slot == 142)
}

/// Whether a list call through `slot` records the list's deferred draws first.
Expand Down Expand Up @@ -1359,6 +1368,12 @@ enum Cmd {
Indirect(usize, usize, u64),
/// the title's SetPipelineState to the pipeline `p`, as without a run (`real::bind_pipeline`)
Bind(usize),
/// the title's OMSetStencilRef between deferred draws, replayed before subsequent mesh passes
StencilRef(u32),
/// the title's IASetPrimitiveTopology between deferred draws, replayed before subsequent mesh passes
Topology(u32),
/// the title's RSSetScissorRects between deferred draws, replayed before subsequent mesh passes
Scissors(Vec<[i32; 4]>),
/// an `XCL_GPU_TIMES` span: the rewrites and compute passes (None), the mesh pass of draw `d`
TimesBegin(Option<usize>),
TimesEnd,
Expand Down Expand Up @@ -1428,6 +1443,18 @@ fn batch_commands(steps: &[Step], records: usize, open: bool) -> Vec<Cmd> {
pso = 0;
continue;
}
Step::StencilRef(r) => {
v.push(Cmd::StencilRef(*r));
continue;
}
Step::Topology(t) => {
v.push(Cmd::Topology(*t));
continue;
}
Step::Scissors(r) => {
v.push(Cmd::Scissors(r.clone()));
continue;
}
Step::Draw(d) => d,
};
v.push(Cmd::TimesBegin(Some(i)));
Expand Down Expand Up @@ -1479,6 +1506,9 @@ unsafe fn emit_commands(this: *mut c_void, list: &ID3D12GraphicsCommandList, cmd
Cmd::DispatchMesh(n) => pc_dm(this, *n, 1, 1),
Cmd::Indirect(sig, buf, at) => pc_ei(this, *sig, 1, *buf, *at, 0, 0),
Cmd::Bind(p) => super::real::bind_pipeline(this, *p as *mut c_void),
Cmd::StencilRef(r) => super::list_x::apply_stencil_ref(this, *r),
Cmd::Topology(t) => super::list_x::apply_topology(this, *t),
Cmd::Scissors(r) => super::list_x::apply_scissors(this, r),
Cmd::TimesBegin(None) => super::gpu_times::before(this, 0, super::gpu_times::Kind::MeshBatch, [draws.len() as u32, 0, 0], false),
Cmd::TimesBegin(Some(d)) => {
let d = &draws[*d];
Expand All @@ -1500,10 +1530,14 @@ pub(super) unsafe fn flush_two_pass(this: *mut c_void, why: RunEnd) {
let (items, records, open) = with(this, |s| (std::mem::take(&mut s.deferred), s.batch_buf.as_ref().map(|b| b.res.clone()), s.batch_open));
let draws: Vec<&DeferredDraw> = items.iter().filter_map(|i| if let RunItem::Draw(d) = i { Some(d) } else { None }).collect();
if draws.is_empty() {
// (pipeline sets only: as they would have gone)
// (queued state sets only: as they would have gone)
for i in &items {
if let RunItem::Bind { pso: p, .. } = i {
super::real::bind_pipeline(this, *p as *mut c_void);
match i {
RunItem::Bind { pso: p, .. } => super::real::bind_pipeline(this, *p as *mut c_void),
RunItem::StencilRef(r) => super::list_x::apply_stencil_ref(this, *r),
RunItem::Topology(t) => super::list_x::apply_topology(this, *t),
RunItem::Scissors(r) => super::list_x::apply_scissors(this, r),
RunItem::Draw(_) => {}
}
}
return;
Expand Down Expand Up @@ -1566,6 +1600,36 @@ unsafe fn batch_region(this: *mut c_void, region: u64) -> Result<u64, String> {
va.map(|va| va + at).ok_or_else(|| "no batch record buffer".into())
}

/// Queue `OMSetStencilRef` into the list's deferred two-pass run when deferring, so it replays
/// in title order between mesh passes instead of flushing the batch.
pub(super) unsafe fn queue_stencil_ref(this: *mut c_void, r: u32) -> bool {
if DEFERRING_LISTS.load(Ordering::Relaxed) == 0 || !DEFERRING.contains(this as usize) {
return false;
}
with(this, |s| s.deferred.push(RunItem::StencilRef(r)));
true
}

/// Queue `IASetPrimitiveTopology` into the list's deferred two-pass run when deferring, so it
/// replays in title order between mesh passes instead of flushing the batch.
pub(super) unsafe fn queue_topology(this: *mut c_void, t: u32) -> bool {
if DEFERRING_LISTS.load(Ordering::Relaxed) == 0 || !DEFERRING.contains(this as usize) {
return false;
}
with(this, |s| s.deferred.push(RunItem::Topology(t)));
true
}

/// Queue `RSSetScissorRects` into the list's deferred two-pass run when deferring, so it
/// replays in title order between mesh passes instead of flushing the batch.
pub(super) unsafe fn queue_scissors(this: *mut c_void, rects: Vec<[i32; 4]>) -> bool {
if DEFERRING_LISTS.load(Ordering::Relaxed) == 0 || !DEFERRING.contains(this as usize) {
return false;
}
with(this, |s| s.deferred.push(RunItem::Scissors(rects)));
true
}

/// `draw` deferred on `this`; the run is recorded when it has `batch_most` draws.
unsafe fn push_deferred(this: *mut c_void, draw: DeferredDraw) {
let n = with(this, |s| {
Expand Down Expand Up @@ -5099,31 +5163,36 @@ void main(uint t : SV_GroupIndex) {
let (mut cv, mut gv) = (BTreeMap::new(), BTreeMap::new());
let mut out = Vec::new();
for c in cmds {
match *c {
match c {
Cmd::ComputeRs(r) => {
let r = *r;
if r != crs {
cv.clear();
}
crs = r;
}
Cmd::GraphicsRs(r) => {
let r = *r;
if r != grs {
gv.clear();
}
grs = r;
}
Cmd::Pso(p) => pso = p,
Cmd::Pso(p) => pso = *p,
Cmd::ComputeView(s, va) => {
cv.insert(s, va);
cv.insert(*s, *va);
}
Cmd::GraphicsView(s, va) => {
gv.insert(s, va);
gv.insert(*s, *va);
}
Cmd::Dispatch(_) | Cmd::Indirect(0xD0, ..) => out.push(Call { call: c.clone(), pso, rs: crs, views: cv.clone() }),
Cmd::DispatchMesh(_) | Cmd::Indirect(..) => out.push(Call { call: c.clone(), pso, rs: grs, views: gv.clone() }),
// (the title's pipeline set, with what it sets: as a call of its own, in order)
Cmd::Bind(p) => {
pso = p;
pso = *p;
out.push(Call { call: c.clone(), pso, rs: 0, views: BTreeMap::new() });
}
Cmd::StencilRef(_) | Cmd::Topology(_) | Cmd::Scissors(_) => {
out.push(Call { call: c.clone(), pso, rs: 0, views: BTreeMap::new() });
}
Cmd::Barriers { .. } | Cmd::TimesBegin(_) | Cmd::TimesEnd => {}
Expand Down Expand Up @@ -5168,6 +5237,9 @@ void main(uint t : SV_GroupIndex) {
for s in &run {
match s {
Step::Bind(p) => alone.push(calls(&[Cmd::Bind(*p)])[0].clone()),
Step::StencilRef(r) => alone.push(calls(&[Cmd::StencilRef(*r)])[0].clone()),
Step::Topology(t) => alone.push(calls(&[Cmd::Topology(*t)])[0].clone()),
Step::Scissors(r) => alone.push(calls(&[Cmd::Scissors(r.clone())])[0].clone()),
Step::Draw(d) => alone.extend(calls(&unbatched_commands(d))),
}
}
Expand All @@ -5194,6 +5266,40 @@ void main(uint t : SV_GroupIndex) {
assert_eq!(cmds.iter().filter(|c| matches!(c, Cmd::Barriers { .. })).count(), 3, "still three ResourceBarrier calls");
}

/// A run with the title's stencil reference, topology and scissor sets between its draws: the
/// sets come among the mesh passes where the title made them (each mesh pass draws with the
/// state of its draw, as the unbatched path gives it), none among the compute passes.
#[test]
fn state_sets_in_a_run_come_between_the_mesh_passes_in_the_titles_order() {
let run = vec![
Step::Draw(snap(0, None)),
Step::StencilRef(3),
Step::Draw(snap(1, Some(0xB1))),
Step::Scissors(vec![[0, 0, 640, 360]]),
Step::Topology(4),
Step::Draw(snap(2, None)),
Step::Scissors(Vec::new()),
Step::Draw(snap(3, None)),
];
let cmds = batch_commands(&run, 0x900, false);
let mut alone = Vec::new();
for s in &run {
match s {
Step::StencilRef(r) => alone.push(Cmd::StencilRef(*r)),
Step::Topology(t) => alone.push(Cmd::Topology(*t)),
Step::Scissors(r) => alone.push(Cmd::Scissors(r.clone())),
Step::Draw(d) => alone.extend(unbatched_commands(d)),
Step::Bind(p) => alone.push(Cmd::Bind(*p)),
}
}
let mesh_side = |c: &[Cmd]| -> Vec<Cmd> {
c.iter().filter(|x| matches!(x, Cmd::StencilRef(_) | Cmd::Topology(_) | Cmd::Scissors(_) | Cmd::DispatchMesh(_) | Cmd::Indirect(0xD1, ..))).cloned().collect()
};
assert_eq!(mesh_side(&cmds), mesh_side(&alone));
let first_mesh = cmds.iter().position(|c| matches!(c, Cmd::TimesBegin(Some(_)))).unwrap();
assert!(!cmds[..first_mesh].iter().any(|c| matches!(c, Cmd::StencilRef(_) | Cmd::Topology(_) | Cmd::Scissors(_))), "no state set among the compute passes");
}

/// SetPipelineState with no deferred draws binds as it always did; with them a two-pass
/// pipeline goes into the run and any other pipeline records the run first. Into the run, it
/// becomes the pipeline the next draws resolve, and nothing is recorded yet.
Expand Down Expand Up @@ -5337,11 +5443,14 @@ void main(uint t : SV_GroupIndex) {
for newer in [false, true] {
let kept: Vec<usize> = (0..600).filter(|&s| keeps_two_pass_batch(s, newer)).collect();
let (ei, dm) = if newer { (61, 129) } else { (59, 127) };
let want: Vec<usize> = (0..=8).chain([25]).chain(33..=46).chain([ei, dm]).collect();
let mut want: Vec<usize> = (0..=8).chain([20, 22, 24, 25]).chain(33..=46).chain([ei, dm]).collect();
if newer {
want.push(142);
}
assert_eq!(kept, want, "newer layout: {newer}");
// pipelines, barriers, draws, dispatches, copies, clears, IA / RS / OM state, heaps,
// queries, predication, bundles, markers, Close, Reset, the console's own methods
for slot in [26, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 27, 28, 29, 30, 31, 32, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 9, 10, 11, 60, 67, 78, 81, 85, 93, 104, 105, 106, 124, 125, 139, 146, 148] {
for slot in [26, 12, 13, 14, 15, 16, 17, 18, 19, 21, 23, 27, 28, 29, 30, 31, 32, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 9, 10, 11, 60, 67, 78, 81, 85, 93, 104, 105, 106, 124, 125, 139, 146, 148] {
assert!(flush_wanted(slot, newer, true, true), "slot {slot}, newer layout: {newer}");
}
// the other layout's DispatchMesh / ExecuteIndirect slots are other methods there
Expand Down
40 changes: 37 additions & 3 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,13 @@ pub(super) unsafe extern "system" fn ia_set_topology_x(this: *mut c_void, t: u32
crate::xlog_warn!("D3D12.X: IASetPrimitiveTopology({t}) – a console topology the PC does not have{}", if t == RECT_LIST { ": rect lists are drawn as triangle lists, completed by a GS in rect list pipelines" } else { "" });
}
}
if super::bind::queue_topology(this, t) {
return;
}
apply_topology(this, t);
}

pub(super) unsafe fn apply_topology(this: *mut c_void, t: u32) {
let rect = t == RECT_LIST;
let t = if t == RECT_LIST { windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST.0 as u32 } else { t };
with_bindings(this, |b| {
Expand Down Expand Up @@ -600,13 +607,33 @@ pub(super) unsafe extern "system" fn rs_set_scissors_x(this: *mut c_void, n: u32
let rs = if r.is_null() { &[][..] } else { std::slice::from_raw_parts(r, n as usize) };
Some(super::capture::Op::Scissors(rs.iter().map(|r| [r.left, r.top, r.right, r.bottom]).collect()))
});
if !r.is_null() {
let rs = std::slice::from_raw_parts(r, n as usize).to_vec();
with_bindings(this, |b| b.scissors = rs);
if !r.is_null() && n > 0 {
let rs = std::slice::from_raw_parts(r, n as usize);
let rects: Vec<[i32; 4]> = rs.iter().map(|r| [r.left, r.top, r.right, r.bottom]).collect();
if super::bind::queue_scissors(this, rects) {
return;
}
with_bindings(this, |b| b.scissors = rs.to_vec());
} else if r.is_null() || n == 0 {
if super::bind::queue_scissors(this, Vec::new()) {
return;
}
}
pc::<unsafe extern "system" fn(*mut c_void, u32, *const windows::Win32::Foundation::RECT)>(this, PC_RS_SET_SCISSOR_RECTS)(this, n, r)
}

pub(super) unsafe fn apply_scissors(this: *mut c_void, rects: &[[i32; 4]]) {
let rs: Vec<windows::Win32::Foundation::RECT> = rects
.iter()
.map(|&[left, top, right, bottom]| windows::Win32::Foundation::RECT { left, top, right, bottom })
.collect();
if !rs.is_empty() {
with_bindings(this, |b| b.scissors = rs.clone());
}
let ptr = if rs.is_empty() { std::ptr::null() } else { rs.as_ptr() };
pc::<unsafe extern "system" fn(*mut c_void, u32, *const windows::Win32::Foundation::RECT)>(this, PC_RS_SET_SCISSOR_RECTS)(this, rs.len() as u32, ptr)
}

/// console #23 OMSetBlendFactor (PC #23)
pub(super) unsafe extern "system" fn om_set_blend_factor_x(this: *mut c_void, f: *const [f32; 4]) {
let v = f.as_ref().copied();
Expand All @@ -616,6 +643,13 @@ pub(super) unsafe extern "system" fn om_set_blend_factor_x(this: *mut c_void, f:

/// console #24 OMSetStencilRef (PC #24)
pub(super) unsafe extern "system" fn om_set_stencil_ref_x(this: *mut c_void, r: u32) {
if super::bind::queue_stencil_ref(this, r) {
return;
}
apply_stencil_ref(this, r);
}

pub(super) unsafe fn apply_stencil_ref(this: *mut c_void, r: u32) {
with_bindings(this, |b| b.stencil = Some(r));
pc::<unsafe extern "system" fn(*mut c_void, u32)>(this, PC_OM_SET_STENCIL_REF)(this, r)
}
Expand Down
Loading