Skip to content
Merged
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
153 changes: 151 additions & 2 deletions d3d12/crates/leonida-gpu/src/descriptor/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6317,6 +6317,22 @@ impl Copied<'_> {
/// Most of a changed page is as it was: it is looked at sixteen records at a
/// time.
fn changed_records(last: &mut [u8], records: &[u8], step: usize, first: u32, out: &mut Vec<u32>) {
#[cfg(target_arch = "x86_64")]
if step == 32 && last.len() == records.len() && std::arch::is_x86_feature_detected!("avx2") {
// SAFETY: AVX2 is verified above; `last` and `records` have equal length.
unsafe { changed_records_avx2_32(last, records, first, out) };
return;
}
changed_records_scalar(last, records, step, first, out);
}

fn changed_records_scalar(
last: &mut [u8],
records: &[u8],
step: usize,
first: u32,
out: &mut Vec<u32>,
) {
const AT_A_TIME: usize = 16;
let block = AT_A_TIME * step;
for (b, (old, new)) in last.chunks_mut(block).zip(records.chunks(block)).enumerate() {
Expand All @@ -6332,19 +6348,106 @@ fn changed_records(last: &mut [u8], records: &[u8], step: usize, first: u32, out
}
}

#[cfg(target_arch = "x86_64")]
#[target_feature(enable = "avx2")]
unsafe fn changed_records_avx2_32(
last: &mut [u8],
records: &[u8],
first: u32,
out: &mut Vec<u32>,
) {
use std::arch::x86_64::{
__m256i, _mm256_and_si256, _mm256_cmpeq_epi8, _mm256_loadu_si256, _mm256_movemask_epi8,
_mm256_storeu_si256,
};

let count = last.len() / 32;
let dst = last.as_mut_ptr();
let src = records.as_ptr();
let mut i = 0usize;

// Four 32-byte descriptor records (128 bytes) per iteration: one combined
// equality check on the fast path, and direct stores of the already-loaded
// vectors when any record differs.
while i + 4 <= count {
let base = i * 32;
// SAFETY: `i + 4 <= count`, so `base + 128 <= last.len() == records.len()`.
unsafe {
let o0 = _mm256_loadu_si256(dst.add(base) as *const __m256i);
let n0 = _mm256_loadu_si256(src.add(base) as *const __m256i);
let o1 = _mm256_loadu_si256(dst.add(base + 32) as *const __m256i);
let n1 = _mm256_loadu_si256(src.add(base + 32) as *const __m256i);
let o2 = _mm256_loadu_si256(dst.add(base + 64) as *const __m256i);
let n2 = _mm256_loadu_si256(src.add(base + 64) as *const __m256i);
let o3 = _mm256_loadu_si256(dst.add(base + 96) as *const __m256i);
let n3 = _mm256_loadu_si256(src.add(base + 96) as *const __m256i);

let e0 = _mm256_cmpeq_epi8(o0, n0);
let e1 = _mm256_cmpeq_epi8(o1, n1);
let e2 = _mm256_cmpeq_epi8(o2, n2);
let e3 = _mm256_cmpeq_epi8(o3, n3);
let all = _mm256_and_si256(_mm256_and_si256(e0, e1), _mm256_and_si256(e2, e3));
if _mm256_movemask_epi8(all) != -1 {
if _mm256_movemask_epi8(e0) != -1 {
_mm256_storeu_si256(dst.add(base) as *mut __m256i, n0);
out.push(first + i as u32);
}
if _mm256_movemask_epi8(e1) != -1 {
_mm256_storeu_si256(dst.add(base + 32) as *mut __m256i, n1);
out.push(first + (i + 1) as u32);
}
if _mm256_movemask_epi8(e2) != -1 {
_mm256_storeu_si256(dst.add(base + 64) as *mut __m256i, n2);
out.push(first + (i + 2) as u32);
}
if _mm256_movemask_epi8(e3) != -1 {
_mm256_storeu_si256(dst.add(base + 96) as *mut __m256i, n3);
out.push(first + (i + 3) as u32);
}
}
}
i += 4;
}

while i < count {
let base = i * 32;
// SAFETY: `i < count`, so `base + 32 <= last.len() == records.len()`.
unsafe {
let o = _mm256_loadu_si256(dst.add(base) as *const __m256i);
let n = _mm256_loadu_si256(src.add(base) as *const __m256i);
if _mm256_movemask_epi8(_mm256_cmpeq_epi8(o, n)) != -1 {
_mm256_storeu_si256(dst.add(base) as *mut __m256i, n);
out.push(first + i as u32);
}
}
i += 1;
}
}

#[cfg(test)]
mod changed_records_tests {
use super::changed_records;
use super::{changed_records, changed_records_scalar};

#[test]
fn changed_records_are_those_that_differ_one_by_one() {
for (step, count) in [(32usize, 128usize), (32, 2048), (16, 77), (32, 5), (32, 17)] {
for (step, count) in [
(32usize, 0usize),
(32, 1),
(32, 3),
(32, 4),
(32, 5),
(32, 17),
(32, 128),
(32, 2048),
(16, 77),
] {
let mut seed = 7u64;
let mut random = move || {
seed = seed.wrapping_mul(0x5851_F42D_4C95_7F2D).wrapping_add(0x1405_7B7E_F767_814F);
(seed >> 33) as usize
};
let mut last: Vec<u8> = (0..step * count).map(|_| random() as u8).collect();
let mut last_scalar = last.clone();
let mut now = last.clone();
let mut differ = Vec::new();
for slot in 0..count {
Expand All @@ -6354,14 +6457,60 @@ mod changed_records_tests {
}
}
let mut out = Vec::new();
let mut out_scalar = Vec::new();
changed_records(&mut last, &now, step, 1000, &mut out);
changed_records_scalar(&mut last_scalar, &now, step, 1000, &mut out_scalar);
assert_eq!(out, differ);
assert_eq!(out, out_scalar);
assert_eq!(last, now);
assert_eq!(last, last_scalar);
out.clear();
changed_records(&mut last, &now, step, 1000, &mut out);
assert!(out.is_empty());
}
}

#[test]
#[ignore = "microbenchmark: cargo test --release -p leonida-gpu --lib changed_records_bench -- --ignored --nocapture"]
fn changed_records_bench() {
const SLOTS: usize = 400_000;
const STEP: usize = 32;
const ROUNDS: usize = 50;
let base: Vec<u8> = (0..SLOTS * STEP).map(|i| (i.wrapping_mul(31) >> 3) as u8).collect();
for (label, dirty_every) in [("0% dirty", 0usize), ("10% dirty (40k/frame)", 10), ("100% dirty", 1)] {
let mut now = base.clone();
if dirty_every > 0 {
for s in (0..SLOTS).step_by(dirty_every) {
now[s * STEP + (s % STEP)] ^= 0x5A;
}
}
let mut out_s = Vec::with_capacity(SLOTS);
let mut out_v = Vec::with_capacity(SLOTS);
let mut t_scalar = std::time::Duration::ZERO;
let mut t_avx2 = std::time::Duration::ZERO;
for _ in 0..ROUNDS {
let mut last_s = base.clone();
out_s.clear();
let t0 = std::time::Instant::now();
changed_records_scalar(&mut last_s, &now, STEP, 0, &mut out_s);
t_scalar += t0.elapsed();

let mut last_v = base.clone();
out_v.clear();
let t1 = std::time::Instant::now();
changed_records(&mut last_v, &now, STEP, 0, &mut out_v);
t_avx2 += t1.elapsed();

assert_eq!(out_s, out_v);
}
let s_us = t_scalar.as_secs_f64() * 1e6 / ROUNDS as f64;
let v_us = t_avx2.as_secs_f64() * 1e6 / ROUNDS as f64;
eprintln!(
"changed_records ({label}, 400k x 32B): scalar={s_us:.1} us/frame, avx2={v_us:.1} us/frame, speedup={:.2}x",
s_us / v_us
);
}
}
}

/// Whether both are shader views of one texture the title placed in
Expand Down