use std::sync::atomic::{AtomicU64, Ordering}; use windows::Win32::Graphics::Direct3D12::*; pub(crate) fn on() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); *ON.get_or_init(|| std::env::var_os("EONACAT_XS_INDIRECT_MERGE").is_some_and(|v| v == "1")) } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct Call { pub(crate) calls: u64, pub(crate) rebinds: [u64; 2], pub(crate) kind: u32, pub(crate) pipeline: usize, pub(crate) single: bool, pub(crate) alone: bool, pub(crate) clamped: bool, pub(crate) epoch: u64, pub(crate) buffer: usize, pub(crate) at: u64, pub(crate) step: u32, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct Run { pub(crate) kind: u32, pub(crate) pipeline: usize, pub(crate) single: bool, pub(crate) alone: bool, pub(crate) buffer: usize, pub(crate) at: u64, pub(crate) step: u32, pub(crate) records: u32, pub(crate) calls: u64, pub(crate) rebinds: [u64; 2], pub(crate) epoch: u64, pub(crate) settled: bool, } impl Run { pub(crate) fn of(call: &Call, epoch: u64) -> Run { Run { kind: call.kind, pipeline: call.pipeline, single: call.single, alone: call.alone, buffer: call.buffer, at: call.at, step: call.step, records: 1, calls: call.calls, rebinds: call.rebinds, epoch, settled: epoch == call.epoch, } } pub(crate) fn join(&mut self, call: &Call, epoch: u64) { self.records += 1; self.calls = call.calls; self.rebinds = call.rebinds; self.settled = epoch == call.epoch; self.epoch = epoch; } pub(crate) fn next(&self) -> u64 { self.at + u64::from(self.records) * u64::from(self.step) } } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum Verdict { Joins, First, Between, Counted, Alone, Differs, Clamped, Rebound, Constants, Unsettled, Apart, } pub(crate) fn verdict(run: Option<&Run>, call: &Call) -> Verdict { let Some(run) = run else { return Verdict::First; }; if call.calls != run.calls { Verdict::Between } else if !call.single || !run.single { Verdict::Counted } else if call.alone || run.alone { Verdict::Alone } else if call.kind != run.kind || call.pipeline != run.pipeline { Verdict::Differs } else if call.clamped { Verdict::Clamped } else if call.rebinds[1] != run.rebinds[1] { Verdict::Rebound } else if call.rebinds[0] != run.rebinds[0] { Verdict::Constants } else if !run.settled || call.epoch != run.epoch { Verdict::Unsettled } else if call.buffer != run.buffer || call.step != run.step || call.at != run.next() { Verdict::Apart } else { Verdict::Joins } } pub(crate) struct Held { pub(crate) signature: ID3D12CommandSignature, pub(crate) buffer: ID3D12Resource, } impl Held { pub(crate) fn record(&self, list: &ID3D12GraphicsCommandList, run: &Run) { unsafe { list.ExecuteIndirect(&self.signature, run.records, &self.buffer, run.at, None, 0) }; } } static COUNTS: [AtomicU64; 12] = [const { AtomicU64::new(0) }; 12]; const JOINED: usize = 11; pub(crate) fn count(verdict: Verdict, joined: bool) { COUNTS[verdict as usize].fetch_add(1, Ordering::Relaxed); if joined { COUNTS[JOINED].fetch_add(1, Ordering::Relaxed); } } pub(crate) fn line(frames: f64) { let [joins, first, between, counted, alone, differs, clamped, rebound, constants, unsettled, apart, joined] = std::array::from_fn::(|i| COUNTS[i].swap(0, Ordering::Relaxed) as f64 / frames); let all = joins + first + between + counted + alone + differs + clamped + rebound + constants + unsettled + apart; if all == 0.0 { return; } tracing::info!( "indirect executions per frame: {all:.0}, {joins:.0} could join the one before them in one execution \ (EONACAT_XS_INDIRECT_MERGE=1 joins them: now {}), {joined:.0} joined; of the others {first:.0} first on their \ list, {between:.0} after a title call that records, {counted:.0} with a count buffer or more records (theirs \ or the one's before), {alone:.0} kept alone, {differs:.0} of another kind or pipeline, {clamped:.0} clamped \ by a pass of their own, {rebound:.0} after the title changed their binding, {constants:.0} after it changed \ root constants only, {unsettled:.0} after a binding that changed the list, {apart:.0} with their record apart", if on() { "on" } else { "off" } ); }