Initial version
This commit is contained in:
1 parent
2cff35f215
commit
fe8e2cb5e7
360 files changed
+253989
-74
No files matched your search
@@ -0,0 +1,346 @@
|
||||
|
||||
|
||||
use windows::Win32::Graphics::Direct3D12::*;
|
||||
|
||||
use super::{Clamp, pre_clamp_barriers};
|
||||
use crate::host::transition;
|
||||
|
||||
pub(crate) const JOBS: u32 = 256;
|
||||
|
||||
pub(crate) const TABLE: u64 = 16 + 16 * JOBS as u64;
|
||||
|
||||
pub(crate) const SLOT: u32 = 32;
|
||||
|
||||
pub(crate) const SOURCES: usize = 4;
|
||||
|
||||
pub(crate) const TIMES: u32 = 31;
|
||||
|
||||
pub(crate) const SOURCE: &str = r#"
|
||||
#define RS "SRV(t0), SRV(t1), SRV(t2), SRV(t3), SRV(t4), UAV(u0), UAV(u1)"
|
||||
ByteAddressBuffer src0 : register(t0);
|
||||
ByteAddressBuffer src1 : register(t1);
|
||||
ByteAddressBuffer src2 : register(t2);
|
||||
ByteAddressBuffer src3 : register(t3);
|
||||
|
||||
ByteAddressBuffer jobs : register(t4);
|
||||
RWByteAddressBuffer dst_args : register(u0);
|
||||
RWByteAddressBuffer budget : register(u1);
|
||||
static const uint BUDGET = 1u << 27;
|
||||
static const uint GROUPS = 1u << 20;
|
||||
groupshared uint costs[64];
|
||||
groupshared uint fits[64];
|
||||
groupshared uint largest_asked;
|
||||
groupshared uint all_asked;
|
||||
uint load(uint source, uint at) {
|
||||
if (source == 0) return src0.Load(at);
|
||||
if (source == 1) return src1.Load(at);
|
||||
if (source == 2) return src2.Load(at);
|
||||
return src3.Load(at);
|
||||
}
|
||||
[RootSignature(RS)]
|
||||
[numthreads(64, 1, 1)]
|
||||
void main(uint3 id : SV_DispatchThreadID, uint gi : SV_GroupIndex) {
|
||||
uint count = jobs.Load(0);
|
||||
bool active = id.x < count;
|
||||
uint4 job = uint4(0, 0, 0, 0);
|
||||
uint record[5] = {0, 0, 0, 0, 0};
|
||||
uint cost = 0;
|
||||
uint asked = 0;
|
||||
uint kind = 0;
|
||||
uint words = 0;
|
||||
if (gi == 0) {
|
||||
largest_asked = 0;
|
||||
all_asked = 0;
|
||||
}
|
||||
|
||||
if (active) {
|
||||
job = jobs.Load4(16 + id.x * 16);
|
||||
kind = (job.x >> 4) & 15;
|
||||
words = min((job.x >> 8) & 15, 5);
|
||||
for (uint w = 0; w < words; w++) {
|
||||
record[w] = load(job.x & 3, job.y + w * 4);
|
||||
}
|
||||
if (kind <= 1) {
|
||||
if (record[0] >= 1u << 23) {
|
||||
record[0] = 0;
|
||||
record[1] = 0;
|
||||
}
|
||||
record[1] = min(record[1], (1u << 26) / max(record[0], 1u));
|
||||
cost = record[0] * record[1];
|
||||
} else if (kind == 2) {
|
||||
uint3 g = uint3(record[0], record[1], record[2]);
|
||||
g = min(g, uint3(g.x <= WIDEST ? WIDEST : 65535u, 65535u, 65535u));
|
||||
if ((g.y != 0 && g.x > GROUPS / g.y) || (g.z != 0 && g.x * g.y > GROUPS / g.z)) {
|
||||
g = uint3(0, 0, 0);
|
||||
}
|
||||
cost = g.x * g.y * g.z * 64u;
|
||||
record[0] = g.x;
|
||||
record[1] = g.y;
|
||||
record[2] = g.z;
|
||||
} else {
|
||||
|
||||
if (record[0] > 65535u) {
|
||||
record[0] = 0;
|
||||
record[1] = 0;
|
||||
record[2] = 0;
|
||||
} else {
|
||||
record[1] = 1;
|
||||
record[2] = 1;
|
||||
}
|
||||
asked = record[0];
|
||||
cost = record[0] * 128u;
|
||||
}
|
||||
cost *= job.x >> 16;
|
||||
}
|
||||
costs[gi] = cost;
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
if (active && kind > 2) {
|
||||
uint was;
|
||||
InterlockedMax(largest_asked, asked, was);
|
||||
InterlockedAdd(all_asked, min(asked, 1u << 24), was);
|
||||
}
|
||||
uint first = id.x - gi;
|
||||
uint n = count > first ? min(64u, count - first) : 0u;
|
||||
if (gi == 0 && n > 0) {
|
||||
uint before;
|
||||
budget.InterlockedOr(0, 0, before);
|
||||
[allow_uav_condition]
|
||||
while (before < BUDGET) {
|
||||
uint next = before;
|
||||
for (uint j = 0; j < n; j++) {
|
||||
uint c = costs[j];
|
||||
if (c > 0 && next < BUDGET) {
|
||||
next = (BUDGET - next < c) ? (REST ? BUDGET : next) : (next + c);
|
||||
}
|
||||
}
|
||||
if (next == before) break;
|
||||
uint prev;
|
||||
budget.InterlockedCompareExchange(0, before, next, prev);
|
||||
if (prev == before) break;
|
||||
before = prev;
|
||||
}
|
||||
uint at = before;
|
||||
uint refused = 0;
|
||||
uint biggest = 0;
|
||||
for (uint k = 0; k < n; k++) {
|
||||
uint c = costs[k];
|
||||
uint fit = 1;
|
||||
if (c > 0) {
|
||||
fit = (at < BUDGET && BUDGET - at >= c) ? 1u : 0u;
|
||||
at = (at >= BUDGET || BUDGET - at < c) ? (REST ? BUDGET : at) : (at + c);
|
||||
refused += 1 - fit;
|
||||
biggest = max(biggest, c);
|
||||
}
|
||||
fits[k] = fit;
|
||||
}
|
||||
|
||||
uint told;
|
||||
if (refused > 0) {
|
||||
budget.InterlockedAdd(12, refused, told);
|
||||
}
|
||||
if (biggest >= (1u << 22)) {
|
||||
budget.InterlockedMax(16, biggest, told);
|
||||
}
|
||||
}
|
||||
GroupMemoryBarrierWithGroupSync();
|
||||
if (gi == 0 && n > 0 && all_asked > 0) {
|
||||
uint was;
|
||||
budget.InterlockedMax(4, largest_asked, was);
|
||||
budget.InterlockedAdd(8, all_asked, was);
|
||||
}
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
if (fits[gi] == 0) {
|
||||
record[0] = 0;
|
||||
record[1] = 0;
|
||||
if (kind > 1) {
|
||||
record[2] = 0;
|
||||
}
|
||||
}
|
||||
for (uint s = 0; s < words; s++) {
|
||||
dst_args.Store(job.z + s * 4, record[s]);
|
||||
}
|
||||
}
|
||||
"#;
|
||||
|
||||
pub(crate) fn words(kind: u32) -> Option<u32> {
|
||||
super::min_stride(kind).map(|bytes| bytes / 4)
|
||||
}
|
||||
|
||||
pub(crate) fn job(source: u32, kind: u32, times: u32) -> u32 {
|
||||
source | (kind << 4) | (words(kind).unwrap_or(0) << 8) | (times << 16)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Table {
|
||||
|
||||
at: usize,
|
||||
|
||||
base: u32,
|
||||
|
||||
sources: [u64; SOURCES],
|
||||
|
||||
times: Vec<u32>,
|
||||
|
||||
known: std::collections::HashMap<(u32, u32, u32), u32>,
|
||||
}
|
||||
|
||||
impl Table {
|
||||
|
||||
pub(crate) unsafe fn new(at: *mut u8, base: u32, sources: [u64; SOURCES]) -> Table {
|
||||
|
||||
unsafe { at.cast::<u32>().write_volatile(0) };
|
||||
Table { at: at as usize, base, sources, ..Table::default() }
|
||||
}
|
||||
|
||||
pub(crate) fn place(&mut self, id: u64, offset: u64, kind: u32) -> Option<u64> {
|
||||
let source = self.sources.iter().position(|&s| s == id)? as u32;
|
||||
let offset = u32::try_from(offset).ok().filter(|o| o.is_multiple_of(4))?;
|
||||
offset.checked_add(words(kind)? * 4)?;
|
||||
let key = (source, offset, kind);
|
||||
let table = self.at as *mut u32;
|
||||
let place = |i: u32| u64::from(self.base + i * SLOT);
|
||||
if let Some(&i) = self.known.get(&key)
|
||||
&& self.times[i as usize] < TIMES
|
||||
{
|
||||
self.times[i as usize] += 1;
|
||||
|
||||
unsafe { table.add(4 + 4 * i as usize).write_volatile(job(source, kind, self.times[i as usize])) };
|
||||
return Some(place(i));
|
||||
}
|
||||
let i = self.times.len() as u32;
|
||||
if i == JOBS {
|
||||
return None;
|
||||
}
|
||||
self.times.push(1);
|
||||
self.known.insert(key, i);
|
||||
|
||||
unsafe {
|
||||
let to = table.add(4 + 4 * i as usize);
|
||||
to.write_volatile(job(source, kind, 1));
|
||||
to.add(1).write_volatile(offset);
|
||||
to.add(2).write_volatile(self.base + i * SLOT);
|
||||
to.add(3).write_volatile(0);
|
||||
table.write_volatile(i + 1);
|
||||
}
|
||||
Some(place(i))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct WriteLog {
|
||||
|
||||
ranges: Vec<(u64, u64)>,
|
||||
full: bool,
|
||||
}
|
||||
|
||||
impl WriteLog {
|
||||
const MOST: usize = 4096;
|
||||
|
||||
pub(crate) fn note(&mut self, address: u64, len: u64) {
|
||||
let end = address.saturating_add(len);
|
||||
if self.full || end == address {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(last) = self.ranges.last_mut()
|
||||
&& address <= last.1
|
||||
&& last.0 <= end
|
||||
{
|
||||
*last = (last.0.min(address), last.1.max(end));
|
||||
return;
|
||||
}
|
||||
if self.ranges.iter().any(|&(a, b)| a <= address && end <= b) {
|
||||
return;
|
||||
}
|
||||
if self.ranges.len() == Self::MOST {
|
||||
self.ranges = Vec::new();
|
||||
self.full = true;
|
||||
return;
|
||||
}
|
||||
self.ranges.push((address, end));
|
||||
}
|
||||
|
||||
pub(crate) fn touches(&self, address: u64, len: u64) -> bool {
|
||||
let end = address.saturating_add(len);
|
||||
self.full || self.ranges.iter().any(|&(a, b)| address < b && a < end)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn record(
|
||||
c: &Clamp,
|
||||
l: &ID3D12GraphicsCommandList,
|
||||
(dst, before): (&ID3D12Resource, D3D12_RESOURCE_STATES),
|
||||
(budget_buf, budget_gpu): (&ID3D12Resource, u64),
|
||||
(source_bufs, sources): (&[&ID3D12Resource], [u64; SOURCES]),
|
||||
jobs: u64,
|
||||
wait: bool,
|
||||
) {
|
||||
#[cfg(feature = "diagnostics")]
|
||||
let waits = crate::copystat::barrier_test() & 2 == 0;
|
||||
#[cfg(not(feature = "diagnostics"))]
|
||||
let waits = true;
|
||||
let mut uavs: [&ID3D12Resource; SOURCES + 1] = [budget_buf; SOURCES + 1];
|
||||
let n = source_bufs.len().min(SOURCES);
|
||||
uavs[..n].copy_from_slice(&source_bufs[..n]);
|
||||
uavs[n] = budget_buf;
|
||||
let (first, first_len) =
|
||||
pre_clamp_barriers::<{ SOURCES + 2 }>(&uavs[..=n], dst, before, wait && waits);
|
||||
let after = transition(
|
||||
dst,
|
||||
D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
|
||||
D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT,
|
||||
);
|
||||
|
||||
unsafe {
|
||||
crate::barstat::rb(l, &first[..first_len]);
|
||||
l.SetComputeRootSignature(&c.root);
|
||||
l.SetPipelineState(&c.pso);
|
||||
for (i, source) in sources.into_iter().enumerate() {
|
||||
l.SetComputeRootShaderResourceView(i as u32, source);
|
||||
}
|
||||
l.SetComputeRootShaderResourceView(SOURCES as u32, jobs);
|
||||
l.SetComputeRootUnorderedAccessView(SOURCES as u32 + 1, dst.GetGPUVirtualAddress());
|
||||
l.SetComputeRootUnorderedAccessView(SOURCES as u32 + 2, budget_gpu);
|
||||
l.Dispatch(JOBS.div_ceil(64), 1, 1);
|
||||
|
||||
crate::barstat::rb(l, &[after]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::WriteLog;
|
||||
|
||||
#[test]
|
||||
fn write_log() {
|
||||
let mut log = WriteLog::default();
|
||||
assert!(!log.touches(0x1000, 16));
|
||||
log.note(0x1000, 0x100);
|
||||
log.note(0x1000, 0x100);
|
||||
log.note(0x1080, 0x100);
|
||||
assert_eq!(log.ranges, [(0x1000, 0x1180)]);
|
||||
assert!(log.touches(0x1170, 16));
|
||||
assert!(log.touches(0xff8, 16));
|
||||
assert!(!log.touches(0x1180, 16));
|
||||
assert!(!log.touches(0xff0, 16));
|
||||
log.note(0x8000, 0);
|
||||
assert!(!log.touches(0x8000, 4));
|
||||
log.note(0x9000, 0x10);
|
||||
log.note(0x1010, 0x10);
|
||||
assert_eq!(log.ranges, [(0x1000, 0x1180), (0x9000, 0x9010)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn write_log_full() {
|
||||
let mut log = WriteLog::default();
|
||||
for i in 0..WriteLog::MOST as u64 {
|
||||
log.note(i * 0x100, 0x10);
|
||||
}
|
||||
assert!(!log.touches(0x80, 16));
|
||||
log.note(u64::MAX - 0x100, 0x10);
|
||||
assert!(log.full);
|
||||
assert!(log.touches(0x80, 16));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user