🐛 Say a cache's cleared zeros written to its surface's memory

On Windows, where no picture is drawn into a video cache's surface over
Direct3D 11 textures, the textures are cleared alone to the zeros they owe: a
draw that failed, before the CPU's copy lands the picture, and a command
buffer whose decode answered nothing, at its finish. A drawn picture's writes
were said done to the surface's memory (ImportedSurfaceMemory::
external_writes_done) once their wait returned, but these clears were not.
They are now, after the clear's wait, so the platform's later uses of the
planes acquire what it wrote.

DrawnPlanes::unwritten takes the surface's memory and says the planes written
once the clear is done; a clear that failed is logged and not said written.
The failed draw's path is DrawnPlanes::not_drawn, which DrawnPlanes::draw now
calls with the memory the landing passes. The cache's finish gets each undrawn
surface's memory with its textures (SurfaceCaches::undrawn).

At the pinned runtime the call records nothing for a Direct3D 11 surface: each
submission that uses a plane acquires it (rt_platform's d3d11_surface module
doc). So what the guest sees does not change with this commit. A runtime that
hands the planes over at the call sees the zeros.

The testkit's fake platform keeps no record of this call, so the tests count
the calls on a surface memory of their own:
- a_picture_not_drawn_says_its_zeros_written_to_the_surface: the failed
  draw's path clears the zeros and says them written, once.
- a_drawn_cache_no_picture_lands_in_owes_nothing_after_the_finish now counts
  the calls made before the commit: one where the decode answered nothing,
  none where it answered no picture and the cache's fill wrote the planes.
Both need Direct3D 11 and are ignored off the Windows host.
This commit is contained in:
Anees Iqbal committed 2026-11-26 23:09:07 -08:00
1 parent dc0e699e6e
commit 72dd3db9d4
4 files changed
+115 -31

No files matched your search

@@ -8076,11 +8076,11 @@ impl<P: Platform> Caches<P> {
};
let (signalled, buffers) = signalled;
// Textures no picture is drawn into are cleared to the zeros they owe before the commit,
// with the lock let go of, so every later use of their caches reads them
// (`exported::undrawn`).
// with the lock let go of, and said written to their surfaces' memory, so every later
// use of their caches reads them (`exported::undrawn`).
#[cfg(windows)]
for drawn in undrawn {
drawn.unwritten();
for (drawn, memory) in undrawn {
drawn.unwritten(&*memory);
}
let done = signalled.and_then(|()| commit());
if done.is_ok() {
@@ -894,13 +894,18 @@ impl<P: Platform> SurfaceCaches<P> {
/// hold their make's zeros where a decode took their first fill as met
/// ([`super::SurfaceCache::zeros`]) as a command buffer that used them finishes, and that
/// owe zeros: no decode answered a picture over them, so none is drawn, and the caller
/// clears them to the zeros a fill would have left before the buffer's commit (module doc).
pub(super) fn undrawn(&self, counts: &[super::Count]) -> Vec<Arc<DrawnPlanes>> {
/// clears them to the zeros a fill would have left before the buffer's commit, and says them
/// written to the surface's memory, which each comes with (module doc).
pub(super) fn undrawn(
&self,
counts: &[super::Count],
) -> Vec<(Arc<DrawnPlanes>, Arc<dyn ImportedSurfaceMemory>)> {
(counts.iter())
.filter_map(|count| {
let cache = self.by_block.get(&count.block)?;
let made = cache.surface.made.as_ref().filter(|_| cache.zeros)?;
made.drawn.owes_zeros().then(|| Arc::clone(&made.drawn))
(made.drawn.owes_zeros())
.then(|| (Arc::clone(&made.drawn), Arc::clone(&made.memory)))
})
.collect()
}
@@ -1195,15 +1200,26 @@ mod drawn_tests {
use super::super::tests::cached;
use super::*;
/// The memory of the rig's surface, whose writes the tests make none of.
struct Unwritten;
/// The memory of the rig's surface: how many times its planes were said written.
#[derive(Default)]
struct Told {
done: std::sync::atomic::AtomicUsize,
}
impl ImportedSurfaceMemory for Unwritten {
impl ImportedSurfaceMemory for Told {
fn external_writes_done(&self) -> Result<(), String> {
self.done.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
Ok(())
}
}
impl Told {
/// How many times the planes were said written.
fn times(&self) -> usize {
self.done.load(std::sync::atomic::Ordering::SeqCst)
}
}
/// The planes of the rig's 64 by 48 NV12 surface in its pages: the luma's rows, then the
/// chroma's.
const PLANES: [PagesPlane; 2] = [
@@ -1223,9 +1239,9 @@ mod drawn_tests {
/// The rig's cache made for the host's video, its surface's planes over the Direct3D 11
/// textures `draw` makes, its plane shapes owing their fills from the buffer, as
/// [`SurfaceCaches::add_drawn_planes`] leaves a new surface's: the textures, and the CPU
/// generation of the first fill the cache owes.
fn drawn_cache(r: &Rig, draw: &Arc<PlaneDraw>) -> (Arc<DrawnPlanes>, u64) {
/// [`SurfaceCaches::add_drawn_planes`] leaves a new surface's: the textures, the CPU
/// generation of the first fill the cache owes, and the surface's memory.
fn drawn_cache(r: &Rig, draw: &Arc<PlaneDraw>) -> (Arc<DrawnPlanes>, u64, Arc<Told>) {
let drawn = Arc::new(DrawnPlanes::new(draw, (64, 48), false).expect("the textures"));
let images: [<FakePlatform as Platform>::Texture; 2] = std::array::from_fn(|n| {
let desc = r.p.texture_descriptor(&TextureDesc {
@@ -1259,13 +1275,14 @@ mod drawn_tests {
.insert_drawn_plane(key, image.clone_ref(), true, 0),
);
}
let memory = Arc::new(Told::default());
cache.surface.made = Some(DrawnSurface {
planes: images,
memory: Arc::new(Unwritten),
memory: Arc::clone(&memory) as Arc<dyn ImportedSurfaceMemory>,
drawn: Arc::clone(&drawn),
life: MemoryLife::new(),
});
(drawn, owed)
(drawn, owed, memory)
}
/// The use of a decode that writes the rig's block at CPU generation `cpu_gen`.
@@ -1308,7 +1325,7 @@ mod drawn_tests {
fn a_decode_into_a_new_drawn_cache_fills_no_plane_shape_and_waits_for_none() {
let r = cached();
let draw = Arc::new(PlaneDraw::new(None).expect("the plane draws"));
let (drawn, owed) = drawn_cache(&r, &draw);
let (drawn, owed, _) = drawn_cache(&r, &draw);
let queue = r.device.new_command_queue().unwrap();
let cb = queue.command_buffer(ON).unwrap().unwrap();
let caches = &r.dec.surface_caches;
@@ -1418,16 +1435,17 @@ mod drawn_tests {
}
/// Where such a decode answers nothing at all, its command buffer's finish clears the
/// textures to the zeros they owe; where it answers no picture, the cache's first fill is
/// made, which fills the plane shapes from the buffer: either way the textures owe nothing
/// after the finish
/// textures to the zeros they owe and says them written to the surface's memory, before the
/// commit; where it answers no picture, the cache's first fill is made, which fills the plane
/// shapes from the buffer, and nothing writes the textures: either way the textures owe
/// nothing after the finish
#[test]
#[ignore = "needs Direct3D 11 on the default adapter: run on the Windows host"]
fn a_drawn_cache_no_picture_lands_in_owes_nothing_after_the_finish() {
let draw = Arc::new(PlaneDraw::new(None).expect("the plane draws"));
for answer in [None, Some(false)] {
let r = cached();
let (drawn, owed) = drawn_cache(&r, &draw);
let (drawn, owed, memory) = drawn_cache(&r, &draw);
let queue = r.device.new_command_queue().unwrap();
let cb = queue.command_buffer(ON).unwrap().unwrap();
let caches = &r.dec.surface_caches;
@@ -1437,8 +1455,19 @@ mod drawn_tests {
if let Some(picture) = answer {
caches.decoded(Some(&r.device), BLOCK, picture).unwrap();
}
(caches.finish(&r.device, 7, None, &cb, uses.unwrap(), || cb.commit(ON))).unwrap();
let told = std::cell::Cell::new(None);
let commit = || {
told.set(Some(memory.times()));
cb.commit(ON)
};
(caches.finish(&r.device, 7, None, &cb, uses.unwrap(), commit)).unwrap();
assert!(!drawn.owes_zeros(), "{answer:?}: owed after the finish");
let cleared = usize::from(answer.is_none());
assert_eq!(
told.get(),
Some(cleared),
"{answer:?}: the planes said written before the commit"
);
let filled = r.dec.caches().counts().shapes.fills;
let expected = if answer.is_some() { 2 } else { 0 };
assert_eq!(filled, expected, "{answer:?}: the plane shapes filled");
+64 -9
View File
@@ -24,7 +24,10 @@
//! to zeros first where its picture is smaller than the planes (reims-vgpu-video's
//! `PlaneTargets::owe_zeros`). Where no picture is drawn, they are cleared alone
//! ([`DrawnPlanes::unwritten`]): a draw that fails, before the CPU's copy, and a command buffer
//! whose decode answered nothing, at its finish. A fill of the shapes from the buffer since
//! whose decode answered nothing, at its finish. Each such clear is waited for and then said
//! written to the surface's memory (`ImportedSurfaceMemory::external_writes_done`), as a draw's
//! writes are, before the CPU's copy or the command buffer's commit that follows it, so that the
//! platform's later uses of the planes see the zeros. A fill of the shapes from the buffer since
//! leaves them owing nothing ([`DrawnPlanes::filled`]). A picture smaller than the planes owes
//! the zeros whatever the textures held, another picture's: its draw clears them first, so that
//! no texel past it keeps what it held (`video_host`'s module docs, "A short picture").
@@ -84,10 +87,15 @@ impl DrawnPlanes {
/// Draws `frame`'s crop into the textures once its decode is done, telling `decoded` between
/// the two (the module doc), over zeros where the crop is smaller than them, and counts it.
/// `Err` says why it was not drawn, the textures
/// cleared to the zeros they owe first ([`Self::unwritten`]), counted ([`UNDRAWN_CODE`]);
/// `decoded` is told only of a decode that is done.
pub(crate) fn draw(&self, frame: &Frame, decoded: &mut dyn FnMut()) -> Result<(), String> {
/// `Err` says why it was not drawn, the textures cleared to the zeros they owe first and
/// said written to `memory`, the surface's ([`Self::not_drawn`]); `decoded` is told only of
/// a decode that is done.
pub(crate) fn draw(
&self,
frame: &Frame,
memory: &dyn ImportedSurfaceMemory,
decoded: &mut dyn FnMut(),
) -> Result<(), String> {
let drawn = frame
.sync()
.map_err(|error| error.to_string())
@@ -103,8 +111,7 @@ impl DrawnPlanes {
.draw(&texture, frame.crop_origin(), &self.targets, extent)
});
if let Err(why) = drawn {
self.unwritten();
return Err(undrawn(why));
return Err(self.not_drawn(memory, why));
}
reims_vgpu_vulkan::answer::count_kind(DRAWN_CODE);
reims_vgpu_vulkan::answer::count_kind_by(DRAWN_KIB_CODE, crop_bytes(frame) >> 10);
@@ -128,15 +135,33 @@ impl DrawnPlanes {
self.targets.owes_zeros()
}
/// A picture whose draw failed, `why`: the textures cleared to the zeros they owe and said
/// written to `memory` ([`Self::unwritten`]) before the CPU's copy lands the picture, and
/// the picture counted ([`UNDRAWN_CODE`]). Answers `why`.
fn not_drawn(&self, memory: &dyn ImportedSurfaceMemory, why: String) -> String {
self.unwritten(memory);
undrawn(why)
}
/// Clears the textures to the zeros they owe, where no picture is drawn into them (the module
/// doc), and returns once the GPU did; nothing where they owe none. A failure is logged.
pub(crate) fn unwritten(&self) {
/// doc), and returns once the GPU did; nothing where they owe none. Then says them written to
/// `memory`, the surface's, so that the platform's later uses of the planes see what the
/// clear wrote. A failure of either is logged; a clear that failed is not said written.
pub(crate) fn unwritten(&self, memory: &dyn ImportedSurfaceMemory) {
if let Err(why) = self.draw.zero_owed(&self.targets) {
log::warn!(
target: "reims_vgpu::graphics", kind:% = why;
"a video cache's Direct3D 11 textures were not cleared to the zeros they owe: \
{why}"
);
return;
}
if let Err(why) = memory.external_writes_done() {
log::warn!(
target: "reims_vgpu::graphics", kind:% = why;
"a video cache's Direct3D 11 textures, cleared to the zeros they owe, were not \
said written to the platform: {why}"
);
}
}
}
@@ -185,8 +210,38 @@ impl<P: Platform> DrawnTarget<P> {
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicUsize, Ordering};
use super::*;
/// A surface's memory: how many times its planes were said written.
#[derive(Default)]
struct Told(AtomicUsize);
impl ImportedSurfaceMemory for Told {
fn external_writes_done(&self) -> Result<(), String> {
self.0.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
/// A picture whose draw failed has the textures cleared to the zeros they owe, and then says
/// them written to the surface's memory, once, before the CPU's copy lands it (the module
/// doc); the picture is counted not drawn, and its draw's why is what the landing logs.
#[test]
#[ignore = "needs Direct3D 11 on the default adapter: run on the Windows host"]
fn a_picture_not_drawn_says_its_zeros_written_to_the_surface() {
let draw = Arc::new(PlaneDraw::new(None).expect("the plane draws"));
let drawn = DrawnPlanes::new(&draw, (64, 48), false).expect("the textures");
drawn.fill_taken();
assert!(drawn.owes_zeros(), "owed before");
let memory = Told::default();
let why = drawn.not_drawn(&memory, "the draw failed".to_owned());
assert_eq!(why, "the draw failed");
assert!(!drawn.owes_zeros(), "the zeros written");
assert_eq!(memory.0.load(Ordering::SeqCst), 1, "said written once");
}
/// Each picture not drawn is counted by its code, one a picture, in the degradation counts,
/// and its draw's why is what the landing logs (the module doc).
#[test]
+1 -1
View File
@@ -1658,7 +1658,7 @@ impl<P: Platform> HostPicture<P> for VaPicture<P> {
#[cfg(windows)]
let undrawn = match (&to.luma.drawn, &to.chroma.drawn) {
(Some(luma), Some(_)) => {
match luma.planes.draw(&self.frame, &mut || to.decode_done()) {
match (luma.planes).draw(&self.frame, &*luma.memory, &mut || to.decode_done()) {
Ok(()) => {
luma.memory.external_writes_done()?;
return Ok(());