release the mapped texture even when the readback fails
This commit is contained in:
+35
-16
@@ -412,46 +412,65 @@ impl TonemapPipeline {
|
|||||||
context.Draw(3, 0);
|
context.Draw(3, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy to the SDR staging texture and read it back.
|
// Copy to the SDR staging texture and read it back. The copy itself is
|
||||||
|
// its own small unsafe block so that Unmap below always runs, whatever
|
||||||
|
// the copy does.
|
||||||
let row_bytes = self.width * 4;
|
let row_bytes = self.width * 4;
|
||||||
|
let mut mapped = D3D11_MAPPED_SUBRESOURCE::default();
|
||||||
#[allow(
|
#[allow(
|
||||||
clippy::multiple_unsafe_ops_per_block,
|
clippy::multiple_unsafe_ops_per_block,
|
||||||
reason = "copy/map/read/unmap must stay paired; the map guard has to be released in the same scope"
|
reason = "the copy and the map are one step: the mapped subresource describes the copy that just happened"
|
||||||
)]
|
)]
|
||||||
// SAFETY: both textures live on this device and share dimensions and
|
// SAFETY: both textures live on this device and share dimensions and
|
||||||
// format, so the copy is well-defined; `mapped` is a zeroed local that
|
// format, so the copy is well-defined; `mapped` is a zeroed local that
|
||||||
// Map fills before any field of it is read, and the slice is sized from
|
// Map fills before any field of it is read.
|
||||||
// the mapped pitch and this pipeline's own height.
|
|
||||||
unsafe {
|
unsafe {
|
||||||
context.CopyResource(&self.staging, &self.render_target);
|
context.CopyResource(&self.staging, &self.render_target);
|
||||||
let mut mapped = D3D11_MAPPED_SUBRESOURCE::default();
|
|
||||||
context.Map(&self.staging, 0, D3D11_MAP_READ, 0, Some(&raw mut mapped))?;
|
context.Map(&self.staging, 0, D3D11_MAP_READ, 0, Some(&raw mut mapped))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The mapped region is released unconditionally after the copy: the
|
||||||
|
// result is held first, then Unmap runs, then the outcome is returned.
|
||||||
|
// Returning straight out of the block below would leave the staging
|
||||||
|
// texture mapped for the life of the process.
|
||||||
|
//
|
||||||
|
// SAFETY: pData points at RowPitch * height bytes for as long as the
|
||||||
|
// mapping is held, and every slice is checked against that length.
|
||||||
|
let copied = unsafe {
|
||||||
let src_slice = std::slice::from_raw_parts(
|
let src_slice = std::slice::from_raw_parts(
|
||||||
mapped.pData.cast::<u8>(),
|
mapped.pData.cast::<u8>(),
|
||||||
(mapped.RowPitch as usize) * (self.height as usize),
|
(mapped.RowPitch as usize) * (self.height as usize),
|
||||||
);
|
);
|
||||||
// RowPitch may exceed width*4 (alignment); copy row by row. Every
|
// RowPitch may exceed width*4 (alignment); copy row by row.
|
||||||
// slice is built from the mapped pitch and this pipeline's own
|
|
||||||
// height, and a short buffer yields an error rather than a panic.
|
|
||||||
let packed = (row_bytes as usize) * (self.height as usize);
|
let packed = (row_bytes as usize) * (self.height as usize);
|
||||||
if mapped.RowPitch == row_bytes {
|
if mapped.RowPitch == row_bytes {
|
||||||
let Some(frame) = src_slice.get(..packed) else {
|
if let Some(frame) = src_slice.get(..packed) {
|
||||||
return Err(anyhow!("mapped frame smaller than expected"));
|
out.write_all(frame).map_err(|_| ())
|
||||||
};
|
} else {
|
||||||
out.write_all(frame)
|
Err(())
|
||||||
.map_err(|e| anyhow!("writing tonemapped frame: {e}"))?;
|
}
|
||||||
} else {
|
} else {
|
||||||
let pitch = mapped.RowPitch as usize;
|
let pitch = mapped.RowPitch as usize;
|
||||||
|
let mut result = Ok(());
|
||||||
for y in 0..(self.height as usize) {
|
for y in 0..(self.height as usize) {
|
||||||
let Some(row) = src_slice.get(y * pitch..y * pitch + row_bytes as usize) else {
|
let Some(row) = src_slice.get(y * pitch..y * pitch + row_bytes as usize) else {
|
||||||
return Err(anyhow!("mapped row {y} outside the mapped buffer"));
|
result = Err(());
|
||||||
|
break;
|
||||||
};
|
};
|
||||||
out.write_all(row)
|
if out.write_all(row).is_err() {
|
||||||
.map_err(|e| anyhow!("writing tonemapped frame: {e}"))?;
|
result = Err(());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// SAFETY: Unmap matches the Map above and runs on every path.
|
||||||
|
unsafe {
|
||||||
context.Unmap(&self.staging, 0);
|
context.Unmap(&self.staging, 0);
|
||||||
}
|
}
|
||||||
|
copied.map_err(|()| anyhow!("tonemapped frame did not fit the mapped buffer"))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user