|
|
@@ -223,6 +223,17 @@ pub(crate) fn read_back(
|
|
|
device: &ID3D12Device,
|
|
|
list: &ID3D12GraphicsCommandList,
|
|
|
texture: &ID3D12Resource,
|
|
|
+) -> Option<(ID3D12Resource, D3D12_PLACED_SUBRESOURCE_FOOTPRINT)> {
|
|
|
+ read_back_of(device, list, texture, 0)
|
|
|
+}
|
|
|
+
|
|
|
+/// As [`read_back`], of subresource `sub` (the stencil plane of a
|
|
|
+/// one-level, one-slice depth-stencil texture is subresource 1).
|
|
|
+fn read_back_of(
|
|
|
+ device: &ID3D12Device,
|
|
|
+ list: &ID3D12GraphicsCommandList,
|
|
|
+ texture: &ID3D12Resource,
|
|
|
+ sub: u32,
|
|
|
) -> Option<(ID3D12Resource, D3D12_PLACED_SUBRESOURCE_FOOTPRINT)> {
|
|
|
let mut footprint = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default();
|
|
|
let mut total = 0;
|
|
|
@@ -231,7 +242,7 @@ pub(crate) fn read_back(
|
|
|
let desc = texture.GetDesc();
|
|
|
device.GetCopyableFootprints(
|
|
|
&desc,
|
|
|
- 0,
|
|
|
+ sub,
|
|
|
1,
|
|
|
0,
|
|
|
Some(&mut footprint),
|
|
|
@@ -282,7 +293,7 @@ pub(crate) fn read_back(
|
|
|
pResource: std::mem::transmute_copy(texture),
|
|
|
Type: D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
|
|
|
Anonymous: D3D12_TEXTURE_COPY_LOCATION_0 {
|
|
|
- SubresourceIndex: 0,
|
|
|
+ SubresourceIndex: sub,
|
|
|
},
|
|
|
};
|
|
|
list.CopyTextureRegion(&to, 0, 0, 0, &from, None);
|
|
|
@@ -384,14 +395,27 @@ fn dump_one(
|
|
|
.ResourceBarrier(&[crate::host::transition(texture, state, copy)]);
|
|
|
}
|
|
|
let read = read_back(&host.device, &list.list, texture);
|
|
|
+ // The stencil plane of a depth-stencil target, which the title's
|
|
|
+ // lighting reads (material class in the low bits; 0x08 "inside a
|
|
|
+ // room", which the lamps of a room are multiplied by): the plane
|
|
|
+ // after the depth plane of a one-level, one-slice texture.
|
|
|
+ let stencil = (matches!(d.Format.0, 19..=22 | 44..=47)
|
|
|
+ && d.MipLevels == 1
|
|
|
+ && d.DepthOrArraySize == 1)
|
|
|
+ .then(|| read_back_of(&host.device, &list.list, texture, 1))
|
|
|
+ .flatten();
|
|
|
if state != copy {
|
|
|
list.list
|
|
|
.ResourceBarrier(&[crate::host::transition(texture, copy, state)]);
|
|
|
}
|
|
|
- read
|
|
|
+ (read, stencil)
|
|
|
};
|
|
|
+ let (read, stencil) = read;
|
|
|
list.close();
|
|
|
host.execute(&[&list]);
|
|
|
+ if let Some((buffer, footprint)) = stencil {
|
|
|
+ stencil_values(&buffer, &footprint, frame, resource);
|
|
|
+ }
|
|
|
let Some((buffer, footprint)) = read else { return };
|
|
|
let f = footprint.Footprint;
|
|
|
let mut data = std::ptr::null_mut();
|
|
|
@@ -435,6 +459,67 @@ fn dump_one(
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/// Logs which values a stencil plane read back by [`read_back_of`] holds
|
|
|
+/// (the most common first), how many texels have each of its bits, and the
|
|
|
+/// box of the texels with bit 3 (0x08).
|
|
|
+fn stencil_values(
|
|
|
+ buffer: &ID3D12Resource,
|
|
|
+ footprint: &D3D12_PLACED_SUBRESOURCE_FOOTPRINT,
|
|
|
+ frame: u64,
|
|
|
+ resource: crate::object::This,
|
|
|
+) {
|
|
|
+ let f = footprint.Footprint;
|
|
|
+ let (w, h, pitch) = (f.Width as usize, f.Height as usize, f.RowPitch as usize);
|
|
|
+ let mut data = std::ptr::null_mut();
|
|
|
+ // SAFETY: the copy finished; the mapping spans the footprint's rows of
|
|
|
+ // one byte a texel, the last row without its padding.
|
|
|
+ unsafe {
|
|
|
+ if h == 0 || w == 0 || buffer.Map(0, None, Some(&mut data)).is_err() {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ let rows = std::slice::from_raw_parts(data.cast::<u8>(), pitch * (h - 1) + w);
|
|
|
+ let mut values = [0u32; 256];
|
|
|
+ let mut bits = [0u32; 8];
|
|
|
+ // Left, top, right, bottom of the texels with bit 3.
|
|
|
+ let mut inside = [u32::MAX, u32::MAX, 0, 0];
|
|
|
+ for y in 0..h {
|
|
|
+ for (x, &v) in rows[y * pitch..y * pitch + w].iter().enumerate() {
|
|
|
+ values[usize::from(v)] += 1;
|
|
|
+ for (bit, count) in bits.iter_mut().enumerate() {
|
|
|
+ *count += u32::from(v >> bit & 1);
|
|
|
+ }
|
|
|
+ if v & 8 != 0 {
|
|
|
+ inside = [
|
|
|
+ inside[0].min(x as u32),
|
|
|
+ inside[1].min(y as u32),
|
|
|
+ inside[2].max(x as u32),
|
|
|
+ inside[3].max(y as u32),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ let mut most: Vec<(usize, u32)> = values
|
|
|
+ .iter()
|
|
|
+ .copied()
|
|
|
+ .enumerate()
|
|
|
+ .filter(|v| v.1 != 0)
|
|
|
+ .collect();
|
|
|
+ most.sort_by_key(|v| std::cmp::Reverse(v.1));
|
|
|
+ most.truncate(16);
|
|
|
+ tracing::info!(
|
|
|
+ frame,
|
|
|
+ ?resource,
|
|
|
+ width = w,
|
|
|
+ height = h,
|
|
|
+ values = ?most,
|
|
|
+ texels_with_bit = ?bits,
|
|
|
+ bit3_box = ?inside,
|
|
|
+ "stencil plane values (value, texels)"
|
|
|
+ );
|
|
|
+ buffer.Unmap(0, None);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
fn half(h: u16) -> f32 {
|
|
|
let (sign, exp, man) = ((h >> 15) as u32, ((h >> 10) & 0x1f) as i32, (h & 0x3ff) as f32);
|
|
|
let v = match exp {
|
|
|
@@ -564,6 +649,42 @@ fn to_rgba(format: u32, w: usize, h: usize, pitch: usize, rows: &[u8]) -> Option
|
|
|
}
|
|
|
let nonzero_bits_percent = set as f32 * 100.0 / (w * h).max(1) as f32;
|
|
|
tracing::info!(format, nonzero_bits_percent, packed_high, "target values as packed light");
|
|
|
+ // The same words as plain numbers, by class. Tables of indices and
|
|
|
+ // masks say nothing as floats or as packed light: in the title's
|
|
|
+ // shadow-mask table 0 is a block in full shadow, a word up to 255
|
|
|
+ // one value for the block, above that a block of the atlas, and all
|
|
|
+ // ones a texel never written.
|
|
|
+ let mut classes = [0usize; 6];
|
|
|
+ let mut highest_below_ones = 0u32;
|
|
|
+ for y in 0..h {
|
|
|
+ for x in 0..w {
|
|
|
+ let v = u32_at(px(x, y, 4), 0);
|
|
|
+ classes[match v {
|
|
|
+ 0 => 0,
|
|
|
+ 1..=15 => 1,
|
|
|
+ 16..=255 => 2,
|
|
|
+ 256..=0x00ff_ffff => 3,
|
|
|
+ u32::MAX => 5,
|
|
|
+ _ => 4,
|
|
|
+ }] += 1;
|
|
|
+ if v != u32::MAX {
|
|
|
+ highest_below_ones = highest_below_ones.max(v);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tracing::info!(
|
|
|
+ format,
|
|
|
+ width = w,
|
|
|
+ height = h,
|
|
|
+ zero = classes[0],
|
|
|
+ one_to_15 = classes[1],
|
|
|
+ to_255 = classes[2],
|
|
|
+ to_24_bits = classes[3],
|
|
|
+ larger = classes[4],
|
|
|
+ all_ones = classes[5],
|
|
|
+ highest_below_ones,
|
|
|
+ "target values as words"
|
|
|
+ );
|
|
|
}
|
|
|
// Float colours (scene light) go through a cube root: scaled linearly,
|
|
|
// a night scene under one lamp of 50 units came out all black.
|