diff --git a/src-tauri/src/capture.rs b/src-tauri/src/capture.rs index 4b10033..ea0b55a 100644 --- a/src-tauri/src/capture.rs +++ b/src-tauri/src/capture.rs @@ -412,46 +412,65 @@ impl TonemapPipeline { 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 mut mapped = D3D11_MAPPED_SUBRESOURCE::default(); #[allow( 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 // 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 - // the mapped pitch and this pipeline's own height. + // Map fills before any field of it is read. unsafe { 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))?; + } + + // 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( mapped.pData.cast::(), (mapped.RowPitch as usize) * (self.height as usize), ); - // RowPitch may exceed width*4 (alignment); copy row by row. Every - // slice is built from the mapped pitch and this pipeline's own - // height, and a short buffer yields an error rather than a panic. + // RowPitch may exceed width*4 (alignment); copy row by row. let packed = (row_bytes as usize) * (self.height as usize); if mapped.RowPitch == row_bytes { - let Some(frame) = src_slice.get(..packed) else { - return Err(anyhow!("mapped frame smaller than expected")); - }; - out.write_all(frame) - .map_err(|e| anyhow!("writing tonemapped frame: {e}"))?; + if let Some(frame) = src_slice.get(..packed) { + out.write_all(frame).map_err(|_| ()) + } else { + Err(()) + } } else { let pitch = mapped.RowPitch as usize; + let mut result = Ok(()); for y in 0..(self.height as usize) { 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) - .map_err(|e| anyhow!("writing tonemapped frame: {e}"))?; + if out.write_all(row).is_err() { + result = Err(()); + break; + } } + result } + }; + + // SAFETY: Unmap matches the Map above and runs on every path. + unsafe { context.Unmap(&self.staging, 0); } + copied.map_err(|()| anyhow!("tonemapped frame did not fit the mapped buffer"))?; Ok(()) } }