diff --git a/src-tauri/src/bootstrap.rs b/src-tauri/src/bootstrap.rs index 6213fcd..7a408d4 100644 --- a/src-tauri/src/bootstrap.rs +++ b/src-tauri/src/bootstrap.rs @@ -360,3 +360,35 @@ fn extract_ffmpeg_from_zip(archive_path: &Path, dest: &Path) -> Result<()> { } bail!("ffmpeg.exe not found inside the zip archive"); } + +#[cfg(test)] +mod tests { + use super::*; + + /// Stands in for the body of extract_ffmpeg_from_7z: it builds a scratch + /// directory, arms the guard, then leaves through the `?` the way a failed + /// extraction does. + fn fails_with_guard_alive(scratch: &Path) -> Result<()> { + fs::create_dir_all(scratch).context("create scratch")?; + let _guard = DirGuard(scratch.to_path_buf()); + fs::write(scratch.join("ffmpeg.exe"), b"x").context("write payload")?; + bail!("deliberate extraction failure") + } + + /// The scratch tree used to be removed only on the success path, so a failed + /// bootstrap left the whole unpacked ffmpeg build sitting in temp/. + #[test] + fn dir_guard_removes_scratch_even_on_early_exit() { + let dir = std::env::temp_dir().join(format!("bytesize-guard-test-{}", std::process::id())); + let scratch = dir.join("temp").join("ffmpeg-extract"); + drop(fs::remove_dir_all(&dir)); + + assert!(fails_with_guard_alive(&scratch).is_err()); + assert!( + !scratch.exists(), + "scratch dir should be gone after the error return" + ); + + drop(fs::remove_dir_all(&dir)); + } +} diff --git a/src-tauri/src/capture.rs b/src-tauri/src/capture.rs index ea0b55a..3c6d062 100644 --- a/src-tauri/src/capture.rs +++ b/src-tauri/src/capture.rs @@ -45,6 +45,16 @@ const MAX_ENCODE_WIDTH: u32 = 3840; const AUTO_ENCODE_WIDTH: u32 = 1920; const FALLBACK_REFRESH_HZ: u32 = 60; +/// Fullscreen-triangle vertices, shared by the vertex-buffer description and the +/// subresource that points at them. Module scope so the returned subresource can +/// reference it without borrowing a stack local. +static VERTS: [f32; 12] = [ + // pos.x pos.y u v + -1.0, -1.0, 0.0, 1.0, // bottom-left + 3.0, -1.0, 2.0, 1.0, // bottom-right (off-screen, completes the triangle) + -1.0, 3.0, 0.0, -1.0, // top-left (off-screen) +]; + /// The cap to apply to the encode width in pixels. /// Auto keeps the historic 1920 cap so nothing changes for existing users; /// Full records at native res up to `MAX_ENCODE_WIDTH` (3840). @@ -95,13 +105,112 @@ struct TonemapPipeline { height: u32, } -// One D3D11 object per descriptor, in the order the API documents them; the -// length is the API's, not ours, and splitting it would scatter a single -// initialization sequence across helpers that each need half the locals. -#[allow( - clippy::too_many_lines, - reason = "linear D3D11 resource setup: each step needs the previous object in scope" -)] +// The D3D11 descriptors are pure data - no device, no unsafe - so they live +// outside the pipeline constructor. Pulling them out keeps that constructor to +// the create-and-store sequence and lets each descriptor read on its own. +fn new_sampler_desc() -> D3D11_SAMPLER_DESC { + // Point-clamp sampler - 1:1 screen-space sampling of the captured frame. + D3D11_SAMPLER_DESC { + Filter: D3D11_FILTER_MIN_MAG_MIP_POINT, + AddressU: D3D11_TEXTURE_ADDRESS_CLAMP, + AddressV: D3D11_TEXTURE_ADDRESS_CLAMP, + AddressW: D3D11_TEXTURE_ADDRESS_CLAMP, + MipLODBias: 0.0, + MaxAnisotropy: 0, + ComparisonFunc: windows::Win32::Graphics::Direct3D11::D3D11_COMPARISON_NEVER, + BorderColor: [0.0; 4], + MinLOD: 0.0, + MaxLOD: 0.0, + } +} + +fn new_rasterizer_desc() -> D3D11_RASTERIZER_DESC { + // No culling. The default rasterizer state culls back faces, which silently + // killed the fullscreen triangle (the winding vs FrontCounterClockwise + // interaction culls the whole tri). CULL_NONE removes that variable. + D3D11_RASTERIZER_DESC { + FillMode: D3D11_FILL_SOLID, + CullMode: D3D11_CULL_NONE, + FrontCounterClockwise: windows::core::BOOL::default(), + DepthBias: 0, + DepthBiasClamp: 0.0, + SlopeScaledDepthBias: 0.0, + DepthClipEnable: windows::core::BOOL::default(), + ScissorEnable: windows::core::BOOL(1), + MultisampleEnable: windows::core::BOOL::default(), + AntialiasedLineEnable: windows::core::BOOL::default(), + } +} + +/// SDR render target (BGRA8). The shader writes gamma-encoded bt709 here. +fn new_render_target_desc(width: u32, height: u32) -> D3D11_TEXTURE2D_DESC { + D3D11_TEXTURE2D_DESC { + Width: width, + Height: height, + MipLevels: 1, + ArraySize: 1, + Format: DXGI_FORMAT_B8G8R8A8_UNORM, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Usage: D3D11_USAGE_DEFAULT, + BindFlags: (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32, + CPUAccessFlags: 0, + MiscFlags: 0, + } +} + +fn new_render_target_view_desc() -> D3D11_RENDER_TARGET_VIEW_DESC { + D3D11_RENDER_TARGET_VIEW_DESC { + Format: DXGI_FORMAT_B8G8R8A8_UNORM, + ViewDimension: D3D11_RTV_DIMENSION_TEXTURE2D, + Anonymous: windows::Win32::Graphics::Direct3D11::D3D11_RENDER_TARGET_VIEW_DESC_0 { + Texture2D: D3D11_TEX2D_RTV { MipSlice: 0 }, + }, + } +} + +/// Staging texture for CPU readback. Same format as the render target. +fn new_staging_desc(width: u32, height: u32) -> D3D11_TEXTURE2D_DESC { + D3D11_TEXTURE2D_DESC { + Width: width, + Height: height, + MipLevels: 1, + ArraySize: 1, + Format: DXGI_FORMAT_B8G8R8A8_UNORM, + SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, + Usage: D3D11_USAGE_STAGING, + BindFlags: 0, + CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32, + MiscFlags: 0, + } +} + +/// 3-vertex buffer for a fullscreen triangle. Each vertex is pos.xy + uv.xy +/// (4 floats = 16 bytes). The triangle covers clip space (-1..1) with UV 0..1. +/// Winding is clockwise for the default D3D11 front-face setting. +/// +/// Returns the buffer description paired with the subresource that points at +/// the vertex data; both have to outlive the `CreateBuffer` call that consumes +/// them. +fn new_vertex_buffer() -> (D3D11_BUFFER_DESC, D3D11_SUBRESOURCE_DATA) { + ( + D3D11_BUFFER_DESC { + // VERTS is a fixed 12-float array, so its size is a compile-time + // constant well inside u32. + ByteWidth: u32::try_from(size_of_val(&VERTS)).unwrap_or(0), + Usage: D3D11_USAGE_IMMUTABLE, + BindFlags: D3D11_BIND_VERTEX_BUFFER.0 as u32, + CPUAccessFlags: 0, + MiscFlags: 0, + StructureByteStride: 0, + }, + D3D11_SUBRESOURCE_DATA { + pSysMem: VERTS.as_ptr().cast::(), + SysMemPitch: 0, + SysMemSlicePitch: 0, + }, + ) +} + impl TonemapPipeline { fn new(device: &ID3D11Device, width: u32, height: u32) -> Result { // Vertex shader: fullscreen-triangle pass-through (no vertex buffer). @@ -149,30 +258,7 @@ impl TonemapPipeline { } let input_layout = input_layout.ok_or_else(|| anyhow!("CreateInputLayout returned null"))?; - // 3-vertex buffer for a fullscreen triangle. Each vertex is pos.xy + uv.xy - // (4 floats = 16 bytes). Triangle covers clip space (-1..1) with UV 0..1. - // Winding is clockwise for default D3D11 front-face (FrontCounterClockwise=false). - let verts: [f32; 12] = [ - // pos.x pos.y u v - -1.0, -1.0, 0.0, 1.0, // bottom-left - 3.0, -1.0, 2.0, 1.0, // bottom-right (off-screen, completes the triangle) - -1.0, 3.0, 0.0, -1.0, // top-left (off-screen) - ]; - let vb_desc = D3D11_BUFFER_DESC { - // verts is a 12-float array, so the size is a compile-time constant - // well inside u32. - ByteWidth: u32::try_from(size_of_val(&verts)).unwrap_or(0), - Usage: D3D11_USAGE_IMMUTABLE, - BindFlags: D3D11_BIND_VERTEX_BUFFER.0 as u32, - CPUAccessFlags: 0, - MiscFlags: 0, - StructureByteStride: 0, - }; - let vb_init = D3D11_SUBRESOURCE_DATA { - pSysMem: verts.as_ptr().cast::(), - SysMemPitch: 0, - SysMemSlicePitch: 0, - }; + let (vb_desc, vb_init) = new_vertex_buffer(); let mut vertex_buffer = None; unsafe { // SAFETY: both descriptions are stack locals that outlive the call, @@ -189,19 +275,7 @@ impl TonemapPipeline { } let ps = ps.ok_or_else(|| anyhow!("CreatePixelShader returned null"))?; - // Point-clamp sampler - 1:1 screen-space sampling of the captured frame. - let sampler_desc = D3D11_SAMPLER_DESC { - Filter: D3D11_FILTER_MIN_MAG_MIP_POINT, - AddressU: D3D11_TEXTURE_ADDRESS_CLAMP, - AddressV: D3D11_TEXTURE_ADDRESS_CLAMP, - AddressW: D3D11_TEXTURE_ADDRESS_CLAMP, - MipLODBias: 0.0, - MaxAnisotropy: 0, - ComparisonFunc: windows::Win32::Graphics::Direct3D11::D3D11_COMPARISON_NEVER, - BorderColor: [0.0; 4], - MinLOD: 0.0, - MaxLOD: 0.0, - }; + let sampler_desc = new_sampler_desc(); let mut sampler = None; unsafe { // SAFETY: sampler_desc is a fully-initialized stack local and the @@ -210,22 +284,7 @@ impl TonemapPipeline { } let sampler = sampler.ok_or_else(|| anyhow!("CreateSamplerState returned null"))?; - // Rasterizer with no culling. The default rasterizer state culls - // back faces, which silently killed the fullscreen triangle (the - // winding vs FrontCounterClockwise interaction culls the whole tri). - // CULL_NONE removes that variable entirely. - let rasterizer_desc = D3D11_RASTERIZER_DESC { - FillMode: D3D11_FILL_SOLID, - CullMode: D3D11_CULL_NONE, - FrontCounterClockwise: windows::core::BOOL::default(), - DepthBias: 0, - DepthBiasClamp: 0.0, - SlopeScaledDepthBias: 0.0, - DepthClipEnable: windows::core::BOOL::default(), - ScissorEnable: windows::core::BOOL(1), - MultisampleEnable: windows::core::BOOL::default(), - AntialiasedLineEnable: windows::core::BOOL::default(), - }; + let rasterizer_desc = new_rasterizer_desc(); let mut rasterizer = None; unsafe { // SAFETY: rasterizer_desc is a fully-initialized stack local and the @@ -235,19 +294,7 @@ impl TonemapPipeline { let rasterizer = rasterizer.ok_or_else(|| anyhow!("CreateRasterizerState returned null"))?; - // SDR render target (BGRA8). The shader writes gamma-encoded bt709 here. - let rt_desc = D3D11_TEXTURE2D_DESC { - Width: width, - Height: height, - MipLevels: 1, - ArraySize: 1, - Format: DXGI_FORMAT_B8G8R8A8_UNORM, - SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, - Usage: D3D11_USAGE_DEFAULT, - BindFlags: (D3D11_BIND_RENDER_TARGET.0 | D3D11_BIND_SHADER_RESOURCE.0) as u32, - CPUAccessFlags: 0, - MiscFlags: 0, - }; + let rt_desc = new_render_target_desc(width, height); let mut render_target = None; unsafe { // SAFETY: rt_desc is a fully-initialized stack local and the @@ -256,13 +303,7 @@ impl TonemapPipeline { } let render_target = render_target.ok_or_else(|| anyhow!("CreateTexture2D rt returned null"))?; - let view_desc = D3D11_RENDER_TARGET_VIEW_DESC { - Format: DXGI_FORMAT_B8G8R8A8_UNORM, - ViewDimension: D3D11_RTV_DIMENSION_TEXTURE2D, - Anonymous: windows::Win32::Graphics::Direct3D11::D3D11_RENDER_TARGET_VIEW_DESC_0 { - Texture2D: D3D11_TEX2D_RTV { MipSlice: 0 }, - }, - }; + let view_desc = new_render_target_view_desc(); let mut rtv = None; unsafe { // SAFETY: render_target was just created on this device and @@ -271,19 +312,7 @@ impl TonemapPipeline { } let rtv = rtv.ok_or_else(|| anyhow!("CreateRenderTargetView returned null"))?; - // Staging texture for CPU readback. Same format as the RT. - let staging_desc = D3D11_TEXTURE2D_DESC { - Width: width, - Height: height, - MipLevels: 1, - ArraySize: 1, - Format: DXGI_FORMAT_B8G8R8A8_UNORM, - SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 }, - Usage: D3D11_USAGE_STAGING, - BindFlags: 0, - CPUAccessFlags: D3D11_CPU_ACCESS_READ.0 as u32, - MiscFlags: 0, - }; + let staging_desc = new_staging_desc(width, height); let mut staging = None; unsafe { // SAFETY: staging_desc is a fully-initialized stack local and the diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 48880f7..7759642 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1089,6 +1089,19 @@ mod tests { } } + /// Windows keeps whatever case a name was created with, so the temp sweep + /// matches extensions case-insensitively. It used to compare with + /// ends_with(".mp4"), which silently missed an upper-case recording. + #[test] + fn extension_match_ignores_case() { + assert!(has_ext("segment-001.mp4", "mp4")); + assert!(has_ext("SEGMENT-001.MP4", "mp4")); + assert!(has_ext("Segment-001.Mp4", "mp4")); + assert!(!has_ext("segment-001.mkv", "mp4")); + assert!(!has_ext("segment-001", "mp4")); + assert!(!has_ext("notsegment.mp4.bak", "mp4")); + } + /// The pcm head-padding step writes a sibling `*.pcm.padtmp` next to the /// concatenated audio. It used to fall outside every sweep pattern, so a /// crash mid-pad left a file the size of the whole audio track in temp/. diff --git a/verify.ps1 b/verify.ps1 index 25ad05f..5887dae 100644 --- a/verify.ps1 +++ b/verify.ps1 @@ -63,7 +63,9 @@ Push-Location $root try { $null = Invoke-Step 'svelte-check (frontend types)' { npm run check } if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo check' { cargo check --manifest-path src-tauri/Cargo.toml } } - if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo clippy' { cargo clippy --manifest-path src-tauri/Cargo.toml } } + # -D warnings matters: clippy exits 0 when it only has warnings, so without + # this the gate passes on a warning-laden build. + if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo clippy' { cargo clippy --manifest-path src-tauri/Cargo.toml -- -D warnings } } if ($failures.Count -eq 0) { $null = Invoke-Step 'cargo test' { cargo test --manifest-path src-tauri/Cargo.toml } } } finally {